0

I have a written a shell script to automate a build process.

The script checksout some code from an SVN repo, compiles and builds the code before extracting the built binary files and storing these in a central location.

I can manually execute the script ./autobuild.sh and it runs perfectly. There are a few sudo commands executed throughout the script, but I echo the password through for the first sudo command and the password holds for the entire time:

echo mypassword! | sudo -S make clean

When I add executing the script as a crontab it fails to complete all the tasks. I've tried to add it as a cronjob for the normal and root users.

Running crontab -e on my normal user account, I want the script to run at ten past midnight every day:

10 0 * * * /home/username/autobuild.sh

Also running a 32-but Cent OS 7 install with all the latest updates installed.

Can anyone provide any suggestions as to why it might work manually but not when run through a cron?

5
  • 1
    Can you share your crontab line?
    – aadarshsg
    Feb 22, 2016 at 8:05
  • @aadarshsg Good shout, added that and my OS. Feb 22, 2016 at 8:08
  • 1
    Cron only provides a limited environment and PATH. To test if that is the problem, run on the command line: env -i HOME=$HOME PATH=/usr/bin /home/username/autobuild.sh. Note that this won't read any of your profile initializations.
    – Jens
    Feb 22, 2016 at 8:11
  • Tried this, it hung on the mv command to move the created binary file. Feb 22, 2016 at 8:34
  • 1
    Hung on mv? Any funny file systems there (NFS, automounter)?
    – Jens
    Feb 22, 2016 at 8:59

2 Answers 2

0

Try this

10 0 * * * /bin/bash /home/username/autobuild.sh
2
  • Getting an error ` sudo: no tty present and no askpass program specified` when I try this. Do I need to echo the password for EACH sudo command? Feb 22, 2016 at 8:22
  • I used this mixed with echoing the user's password each time a sudo command is called echo mypassword! | sudo -S make command. Not elegant, and NOT recommended for live production systems, but it suits for my needs. Feb 22, 2016 at 9:44
0

Step 1. find bash path

:~# whereis bash 

Output

bash: /usr/bin/bash 

step 2. create sh file add the line on top replace with your bash path

#!/usr/bin/bash

step 3. Make the script executable with command chmod +x .

step 4. add cron like this in for every minute to test

crontab -e

*/1 * * * * /usr/bin/bash ~/backup.sh >>test.log
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.