B 脚本在提示符下运行,但在 cron 下不运行

B 脚本在提示符下运行,但在 cron 下不运行

从 cron 运行 bash 脚本时失败并出现以下错误

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

奇怪的是,当从提示符执行时,它运行完美

整个脚本

#!/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"

这是 crontab 条目

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

谢谢 Rob

答案1

如果在 crontab 中使用反引号 (`),则需要转义 crontab 条目中的 % 符号。

像这样:

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

如果您不对其进行转义,它们将被视为换行符。

答案2

您的 EOF 看起来不对,EOF;与您预期的 EOF 不匹配(删除分号)。

[hlinden@dolphin tmp]$ cat dog.sh
/bin/bash #!/bin/bash
猫 <<EOF
结束语;
[hlinden@dolphin tmp]$ sh dog.sh
dog.sh:第 6 行:警告:第 3 行的此处文档由文件末尾分隔(需要“EOF”)
结束语;
[hlinden@dolphin tmp]$ sed -is/\;// dog.sh
[hlinden@dolphin tmp]$ sh dog.sh
[hlinden@dolphin tmp]$

相关内容