Script runs at CLI, but not as a cron job

Status
Not open for further replies.

meshsmith

Cadet
Joined
Sep 11, 2011
Messages
8
Hi everyone

I have a rsync script that I want to run daily. The script works 100% fine when run from the CLI, and I've made sure to use full paths for both files, folders and executables.

I've set up the script to run daily using the FreeNAS GUI, calling the command:
/bin/sh /mnt/storage/utils/cloud-backup-daily.sh

The script itself:
Code:
#!/bin/bash
 
YEAR=`date +"%Y"`
MONTH=`date +"%m-%B"`
FULLDATE=`date +"%Y.%m.%d"`
 
LOGDIR=/mnt/storage/logs/$YEAR/$MONTH
LOGFILE=$LOGDIR/daily-nas-to-cloud-$FULLDATE.txt
EXCLUDELIST=/mnt/storage/utils/exclude-list.txt
BACKUPSRC=/mnt/storage/backups/daily/
BACKUPDEST=(IP address of my backup server):daily/
 
/bin/mkdir -p $LOGDIR
 
/bin/echo "Daily NAS Backup to Cloud:" > $LOGFILE
/bin/echo "" >> $LOGFILE
 
/usr/local/bin/rsync -avHz --progress --delete --exclude-from $EXCLUDELIST --delete-excluded $BACKUPSRC $BACKUPDEST >> $LOGFILE


When run from the CLI, I get a log file named by the current date, starting with the line "Daily NAS Backup to Cloud:" followed by rsync's "sending incremental file list..." and then a list of each file that was backed up by rsync (the output of the rsync command).

However, when run as a Cron job the log file and "Daily NAS Backup to Cloud:" line is there, but none of the rsync output - no "sending incremental file list..." and no files have been copied. The rsync command is obviously not working.

Any ideas where I'm going wrong?

Thanks!
 

Cymike

Staphylococcus
Joined
Jun 24, 2013
Messages
13
What user is the cronjob set to run as, do the permissions on the script match, and what are the actual permissions (ls -l) on the script? Also if you add "set -x" to the script after the /bin/bash line, you should see the full output of what is trying to run.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Two things:

1. The environment that cronjobs run as is not the same environment as you running the command yourself.
2. Why are you using /bin/sh if the script says it should be bash?
 

meshsmith

Cadet
Joined
Sep 11, 2011
Messages
8
Hi Cymike, cyberjock

Thanks very much for the suggestions!

The cronjob user is set to run as root, and the script is owned by root with 770 permissions.

I've added the universal execute permission, and have added "set -x" to the script to see the output. Finally, I've changed the command to "bin/bash". I'll give this a try this evening and report back.
 

meshsmith

Cadet
Joined
Sep 11, 2011
Messages
8
Doh! Okay, got it. I have another script that sets the permissions of the data I want to backup which was changing the rsync script permissions before it ran.

Fixed, working like a charm.
Thanks!
 
Status
Not open for further replies.
Top