Linux
Bash 腳本可以在提示符下工作,但不能在 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
謝謝羅伯
如果在 crontab 中使用反引號 (`),則需要轉義 crontab 條目中的 % 符號。
像這樣:
/home/backup.sh > /home/webserver/logs/BackupLog-`date +\%m\%d\%y`.log
如果您不逃避它們,它們將被視為換行符。
你的 EOF 看起來不對,EOF;與您預期的 EOF 不匹配(刪除分號)。
[hlinden@dolphin tmp]$ cat dog.sh #!/bin/bash 貓 <<EOF 狗 貓 EOF; [hlinden@dolphin tmp]$ sh dog.sh dog.sh:第 6 行:警告:第 3 行的此處文件由文件結尾分隔(需要“EOF”) 狗 貓 EOF; [hlinden@dolphin tmp]$ sed -is/\;// dog.sh [hlinden@dolphin tmp]$ sh dog.sh 狗 貓 [hlinden@dolphin tmp]$