Cron

“在 cron 作業中尋找匹配的 `` 時出現意外的 EOF

  • August 11, 2021

我在執行 RedHat 4.1 的伺服器上設置了一個 cron 作業來備份 MySQL 數據庫,然後上傳到 Amazon S3。目標是將 .bz2 文件放入與星期幾對應的文件夾中。但是,我收到了守護程序郵寄給我的以下錯誤。

定時任務:

MAILTO=backups@example.com
0       4       *       *       *       mysqldump --all-databases  -ubackups -pPassword | gzip > all-databases.sql.bz2; s3cmd put all-databases.sql.bz2 s3://backup_exampleserver.com/mysql_backups/`date +%A`/all-databases.sql.bz2

錯誤資訊:

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

您需要使用反斜杠轉義命令中的百分號:\%,否則將被解釋為命令結束。

來自crontab (5):

The command field (the rest of the line) is the command to be run.  The
entire command portion of the line, up to a newline or % character, will
be executed by /bin/sh or by the shell specified in the SHELL variable of
the crontab.  Percent signs (‘%’) in the command, unless escaped with a
backslash (‘\’), will be changed into newline characters, and all data
after the first ‘%’ will be sent to the command as standard input.

引用自:https://serverfault.com/questions/426748