1

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

 #!/bin/sh
 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
 FILENAME="backup_$TIME.tar.gz"
 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/backup.sh, 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/backup.sh >/dev/null 2>&1) ) but no archive is created. Cron task : 21 1 * * * sh /root/backup.sh >/dev/null 2>&1
Any ideas why this is happening?

1 Answer 1

3

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.

5
  • 1
    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. Dec 19, 2013 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, 2013 at 0:30
  • resulting file was empty Dec 19, 2013 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. Dec 19, 2013 at 1:19
  • Interesting. I just created such a file and got a little bit of stuff.. See pastebin.com/FR2u19gk for what I ran and got in return.
    – kronenpj
    Dec 19, 2013 at 1:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .