I have a script located at /home/user/backup.sh

I'm trying to execute this script via my root crontab. It isn't running. The script works if I create the crontab under my user account.

Is there anyway to run this script as the root crontab? Here is the line I am using.

25 20 * * * sh /home/user/backup.sh

Contents of script:

tar -czvf /home/user/backup/backup.tar.gz /var/www/
mysqldump -u root -p password --all-databases > /home/user/backup/backup.sql
rsync -avz /home/user/backup user@myserver:/home/user/
  • Could it be caused by some permission or system level protection such as selinux? Check the output of sestatus. – Julie Pelletier May 14 '16 at 2:14
  • Is your script perhaps dependent on what directory it is run from, or dependent on environment variables? – Wildcard May 14 '16 at 2:15
  • It's a backup script that creates a tarball and then rsyncs it to a remote VPS. I use absolute paths always. Very basic – user970638 May 14 '16 at 2:17
  • Hmmm. Do you specify the absolute path where the tarball is to be created? (Default is to create in $PWD, I believe; not sure where cron runs out of.) From what you describe above I see no reason the cronjob should fail. Does sudo sh /home/user/backup.sh work? – Wildcard May 14 '16 at 2:20
  • update your question with content of script. monitor your /var/log/cron or /var/log/syslog to see if anything from crond. output your cron like 1 2 * * * /path/to/your/script &>/tmp/script.log. do you have $PATH set in crontab file? – cuongnv23 May 14 '16 at 2:21

root has full access on your system, but doesn't necessarily have all the keys to other systems that your normal user account has.

So the trouble is:

rsync -avz /home/user/backup user@myserver:/home/user/

If you cause root to execute this command as you, your keys will be used and the command will be successful:

sudo -u user rsync -avz /home/user/backup user@myserver:/home/user/

Alternatively, you could install root's public key as an accepted SSH key for user on the myserver system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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