Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got some problem. I have script execution scheduled but when time comes nothing happens.

 TIME=`date +"%d-%m-%Y"`
 cd /var/www/flyer/data/www/
 mysqldump -u root -ppasswd --databases db_name --create-options --default- character-set=utf8 --result-file=dump.sql
 tar -cpzf ../backups/$FILENAME somefolder dump.sql  
 rm dump.sql
 chown flyer:flyer ../backups/$FILENAME
 chmod 666 ../backups/$FILENAME

When I run script directly from from ssh as /root/, everything works as it's supposed to. But when it is ran by cron, nothing happens. grep CRON /var/log/syslog shows that it was executed (Dec 19 01:21:01 vps-8463 /USR/SBIN/CRON[1299]: (root) CMD (sh /root/ >/dev/null 2>&1) ) but no archive is created. Cron task : 21 1 * * * sh /root/ >/dev/null 2>&1
Any ideas why this is happening?

share|improve this question
up vote 3 down vote accepted

My first guess is that the mysqldump command isn't in the path when the script is run by cron. Try:

which mysqldump

Take the output of that and substitute the whole path for the command name. Eg: /usr/bin/mysqldump instead of mysqldump

Or add something to a local PATH variable. Eg: PATH=$PATH:/usr/bin

This will also solve problems if tar, chown, or other commands are not in the default path.

share|improve this answer
Adding SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin to script worked but the weird thing is, these 2 variables are present in /etc/crontab (I just copied them from there). I'd like to know why. – Igor Yavych Dec 19 '13 at 0:26
That depends on exactly how the cron daemon works, but in order to work in general the PATH variable should be set explicitly. You can find out what your cron daemon does to the environment by creating a simple cron script to output the environment to a file. Eg: env > /tmp/foo.txt. – kronenpj Dec 19 '13 at 0:30
resulting file was empty – Igor Yavych Dec 19 '13 at 1:07
Oh, I just used crontab -e and saw that PATH="" (I was adding cron via ispmanager panel). Well, thanks for your help. – Igor Yavych Dec 19 '13 at 1:19
Interesting. I just created such a file and got a little bit of stuff.. See for what I ran and got in return. – kronenpj Dec 19 '13 at 1:20

Your Answer


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.