Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I created a script to do something (after I reduced it just to echo "IS FINALLY WORKING" for testing purpose). If I type

#bash /var/www/html/somedir/script.sh

it works ok.

The problem is that I need a cron to access it and it doesn't work.

To set up the cron I used:

crontab -e

15 * * * * /var/www/html/somedir/script.sh 

or

15 * * * * bash /var/www/html/somedir/script.sh

I see no result at all.

Also I tried to log it

15 * * * * /var/www/html/somedir/script.sh > cronscriptlog.log

The log is empty.

The os is CentOS 5.5.

share|improve this question
    
Note: SELinux is disabled. –  zozo Mar 30 '11 at 7:31
    
I checked the mails and it says the file does not exist (even though the path is correct). –  zozo Mar 30 '11 at 7:43

3 Answers 3

up vote 1 down vote accepted

Make sure /var/www/html/somedir/script.sh doesn't rely on anything outside cron's default PATH. CentOS has the default PATH set to /sbin:/bin:/usr/sbin:/usr/bin.

You may wish to create cron jobs like this:

15 * * * * ( . /etc/profile && /var/www/html/somedir/script.sh )

or

15 * * * * su - user /var/www/html/somedir/script.sh 

to get an execution environment similar to that of your command line.

share|improve this answer

Easy to miss simple things sometimes.

The other thing to check is the cron environment, it'll run it every minute with this so just comment it out after it's given you output once.

* * * * * root env > /tmp/cronenv.log

There's always a difference between the Bash environment and the cron environment.

share|improve this answer
    
Th OP says it runs OK from the command line? –  Iain Mar 30 '11 at 7:44
    
Hadn't finished typing and hit 'submit', whoops :-) –  Jonathan Ross Mar 30 '11 at 7:45
    
Tried adding a user explicity: `15 * * * * root /var/www/html/somedir/script.sh' ? –  Jonathan Ross Mar 30 '11 at 7:51
    
crontab -e implies a user crontab not a system crontab so your second command doesn't work because root isn't a command. –  Iain Mar 30 '11 at 7:52
    
Sorry, I'd forgotten. –  Jonathan Ross Mar 30 '11 at 8:17

Are there any relevant messages in the cron log file /var/log/cron on a CentOS system?

Check the mail for the account that is trying to run the script, that's where cron sends any output it gets from the script.

You have only captured stdout from ypur script you can capture stderr too with &> cronscriptlog.log.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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