[SOLVED] Shell script doesn't run on cron (otherwise it works)
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Shell script doesn't run on cron (otherwise it works)
Hi all,
So I have a VPS where I have the following shell script meant to run every 24 hours (it takes all the databases and certain folders and rsync them to remote server):
backup.sh looks like this:
Code:
#!/bin/sh
# Directory that holds the database backup
DB_BACKUP_DIR=/home/db_backup
# Directories to rsync
DIRS="/var/www $DB_BACKUP_DIR /etc/apache2"
DB_ARCHIVE="$DB_BACKUP_DIR/db_backup_$(date +%a).gz"
/usr/bin/mysqldump --opt -uroot -pmypass --all-databases | gzip > $DB_ARCHIVE
for DIR in $DIRS
do
/usr/local/bin/rsync -rzvt $DIR user@remote-server
done
rm $DB_ARCHIVE
And for the crontab entry I have:
Code:
20 6 * * * /bin/sh /root/backup.sh
The shell script will run normal if I run it from the shell, but it doesn't run from cron. Does anyone have any idea what could be wrong here?
I have this same script on a different VPS and when ran from cron it works ok. But on this VPS it won't work.
Hi all,
So I have a VPS where I have the following shell script meant to run every 24 hours (it takes all the databases and certain folders and rsync them to remote server) backup.sh looks like this:
The shell script will run normal if I run it from the shell, but it doesn't run from cron. Does anyone have any idea what could be wrong here? I have this same script on a different VPS and when ran from cron it works ok. But on this VPS it won't work.
Thanks in advance!
The only possible thing I can see is that you're not putting in the whole path to gzip. Cron may (or may not) supply a path to that command when it runs, depending on the user the job is running as. So, it'll try, the pipe command will fail, and the script will exit.
Try specifying it as "/usr/bin/gzip", or whatever it is on your system. Type in "which gzip" to find out.
It could help if you use this in the beginning of the script
#!/bin/bash -x
Also, remove the /bin/sh in the crontab, and set the execute bit on the script with chmod. You will then run the script in bash debug mode, with lots of lines explaining what happens. And because cron usually sends output as mail, you should get an email with debug information.
Cheers guys! Turns out, I had a problem with "permission denied" when rsync-ing (due to the other VPS overwritten ssh keys). So I had to append ssh key for this VPS so that backup scripts on both VPS work.
So basically script worked fine and cron worked fine. I just wasn't able to see errors! Because I had set MAILTO="" in my crontab.
Glad I opened the topic here. Looks like a really nice community!
Cheers guys! Turns out, I had a problem with "permission denied" when rsync-ing (due to the other VPS overwritten ssh keys). So I had to append ssh key for this VPS so that backup scripts on both VPS work.
So basically script worked fine and cron worked fine. I just wasn't able to see errors! Because I had set MAILTO="" in my crontab.
Glad I opened the topic here. Looks like a really nice community!
Thanks for posting the solution...so many folks never say anything, and don't share their knowledge.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.