LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 02-17-2012, 12:25 PM   #1
kalashari
LQ Newbie
 
Registered: Feb 2012
Posts: 12

Rep: Reputation: Disabled
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.

Thanks in advance!
 
Old 02-17-2012, 12:28 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by kalashari View Post
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.
 
1 members found this post helpful.
Old 02-17-2012, 12:32 PM   #3
kalashari
LQ Newbie
 
Registered: Feb 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
I tried that as well. Didn't work.

Also here's my system $PATH variable: /usr/java/jdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 
Old 02-17-2012, 12:42 PM   #4
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Do you get any errors?
Cron will sent a mail to the owner of the cronjob in case of problems.

Kind regards
 
1 members found this post helpful.
Old 02-17-2012, 12:42 PM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

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.
 
1 members found this post helpful.
Old 02-17-2012, 01:17 PM   #6
kalashari
LQ Newbie
 
Registered: Feb 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
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!
 
1 members found this post helpful.
Old 02-17-2012, 01:50 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by kalashari View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cron doesn't run a certain script mcdorians Linux - Software 12 04-04-2011 02:35 PM
script works when run but not when called by cron grob115 Linux - Server 7 08-28-2010 11:24 AM
Shell Script works different from cron necrolin Programming 2 05-07-2009 09:52 AM
Script works when run manually but not in cron job Harlin Linux - Software 7 10-16-2007 06:36 PM
script doesn't run in cron (runs from shell just fine) Timur Sakayev Linux - Software 6 02-26-2007 12:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration