Linux – Bash script works from prompt but not from cron

bashcronlinuxsftp

When running a bash script from cron it fails with the following error

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file

The strange thing is it works perfectly when executed from the prompt

Whole Script

#!/bin/sh
NOW=$(date +"%b-%d-%y")
bu_PATH="/home/webserver/"

curtime=$(date)

echo ""
echo "Starting backup: $curtime"
echo ""
echo "Moving to $bu_PATH"
cd $bu_PATH

BACKUP_FILE="backup_$NOW.tgz"

echo ""
echo "The backup file will be called $BACKUP_FILE"

RUN_BACKUP="/usr/local/psa/bin/pleskbackup --server --output-file=$bu_PATH$BACKUP_FILE"

echo "Running backup...."
echo `$RUN_BACKUP`

curtime=$(date)

echo "Backup complete: $curtime"

echo ""

echo "Remove older than 7 days of files..."
ssh server '/shares/backup/webserver/cleanup.sh'

curtime=$(date)
echo ""

echo "Connecting to FTP: $curtime"

sftp -b server <<EOF
        cd /shares/backup/webserver/
        put $bu_PATH$BACKUP_FILE
        quit
EOF;

curtime=$(date)
echo "FTP Transfer Complete: $curtime"
echo ""
echo "Cleaning up Local backups"
sh /home/cleanup.sh
echo ""
echo "Clean up log files"
find /home/webserver/logs/*.log -mtime +7 -exec rm {} \;
echo "Job Complete"

This is the crontab entry

/home/backup.sh > /home/webserver/logs/BackupLog-`date +%m%d%y`.log

Thanks
Rob

Best Answer

You need to escape the % signs in the crontab entry if you use backticks (`) in crontab.

like this:

/home/backup.sh > /home/webserver/logs/BackupLog-`date +\%m\%d\%y`.log

They are treated as newline if you dont escape them.