Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
1  
The error clearly states that no matching ` character was found. Is there ` somewhere in your script, are you executing something via backticks? –  Janne Pikkarainen Aug 10 '11 at 10:20
    
Show is the crontab entry –  Iain Aug 10 '11 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 '11 at 14:16
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
This fixed the crontab –  Rob Aug 10 '11 at 10:47
add comment

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]$
share|improve this answer
    
Thanks but I got the same error even without the ; –  Rob Aug 10 '11 at 10:31
1  
Can you post the whole script? –  HampusLi Aug 10 '11 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 '11 at 10:42
    
Thanks I removed the echo and the backticks –  Rob Aug 10 '11 at 10:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.