Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 have very smiple script to do a backup, and it is working fine when I run it from the command line, but I am having problems running it as cron job.

My cron entry is this: (running it every minute for testing atm)

 * * * * * /var/hosting/ctest/ptest/ > /var/tmp/ctest_ptest_backup_cron2.log

And I can see in /var/log/cron that it gets executed

Sep 22 11:06:01 devbox CROND[31506]: (gwesio) CMD (/var/hosting/ctest/ptest/ > /var/tmp/ctest_ptest_backup_cron2.log)

But it's not doing what it is supposed to do, because backup archive is not created. This is how I have set permissions on the file:

-rwxr-xr-x 1 apache developers 877 2011-09-22 10:11 /var/hosting/ctest/ptest/

And the script itself is this:

TIME=$(date +%Y-%m-%d-%H:%M)
BACKUP_FOLDER="$BACKUP_DIR/$(date +%Y-%m-%d-%H%M)"
sudo mkdir $BACKUP_FOLDER
sudo chown apache:developers $BACKUP_FOLDER
sudo chmod g+w $BACKUP_FOLDER
FILENAME="ctest_ptest-$(date +%Y-%m-%d-%H%M).sql"
ARCHIVE_FILENAME="$BACKUP_DIR/ctest_ptest-$(date +%Y-%m-%d-%H%M).tgz"
echo "Database backup script execution"
sudo /usr/bin/mysqldump -uuser -ppassword -hlocalhost --databases ctest_ptest > $PTH
echo "Database dump in $PTH"
echo "Resources backup"
echo "Backup file: $ARCHIVE_FILENAME"
echo "Cleanup"
sudo /bin/rm -r $BACKUP_FOLDER
exit 0

I am running out of ideas where to look for any other indications of what could be wrong. I am trying to use absolute paths everywhere as it is suggested in other posts, and script can be executed without any problems from shell script.

I have tried to debug and commented out rm -r line, and noticed that $BACKUP_FOLDER is not being created in the first place, but I have no idea why. I also tried to change crontab to be su - user /var/hosting.... but it didn't help as well.

Maybe it's something obvious I'm missing here, or there are better ways of doing it? Any ideas?


share|improve this question
Are you able to the run shell script outside of cron? And what happens then? – blauwblaatje Sep 22 '11 at 11:26
up vote 2 down vote accepted

I guess cron is not amused by all those sudo commands. Remove sudo and run the script from cron as root or at least as a user with necessary prileges to needed directories.

share|improve this answer
Cron and sudo work perfectly together... maybe use /usr/bin/sudo – blauwblaatje Sep 22 '11 at 11:26
That was it, removing sudo and running that as root solved my issue. Thanks a lot for a quick response! – Greg Sep 22 '11 at 11:32

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.