3

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

3
  • 1
    The error clearly states that no matching __ character was found. Is there somewhere in your script, are you executing something via backticks? Aug 10, 2011 at 10:20
  • Show is the crontab entry
    – user9517
    Aug 10, 2011 at 10:26
  • as a note, you may be able to use tmpwatch here and there to make the script a bit smaller.
    – Sirex
    Aug 10, 2011 at 14:16

2 Answers 2

9

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.

1
  • This fixed the crontab
    – Rob
    Aug 10, 2011 at 10:47
3

Your EOF looks wrong, EOF; doesn't match your expected EOF (remove the semi-colon).

[hlinden@dolphin tmp]$ cat dog.sh 
#!/bin/bash
cat <<EOF
dog
cat
EOF;
[hlinden@dolphin tmp]$ sh dog.sh 
dog.sh: line 6: warning: here-document at line 3 delimited by end-of-file (wanted `EOF')
dog
cat
EOF;
[hlinden@dolphin tmp]$ sed -i s/\;// dog.sh 
[hlinden@dolphin tmp]$ sh dog.sh
dog
cat
[hlinden@dolphin tmp]$
3
  • Thanks but I got the same error even without the ;
    – Rob
    Aug 10, 2011 at 10:31
  • 1
    Can you post the whole script?
    – HampusLi
    Aug 10, 2011 at 10:33
  • 1
    echo $RUN_BACKUP looks suspicious. If that has a backtick in the output you could get weird results, plus the output will be formatted badly. Why not just have a line with $RUN_BACKUP without the echo and backticks.
    – HampusLi
    Aug 10, 2011 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .