在 cron 中转义双引号和百分号 (%)

在 cron 中转义双引号和百分号 (%)

以下命令在提示符下有效,但在 crontab 下无效。

grep abc /var/log/messages | grep "`date '+%B %d'`" | mail -s"abc log of `hostname`" s.o+`hostname`@gmail.com

我需要将其添加到每日 cron 中。

答案1

您必须避开这些%符号。它们在 crontab 中具有特殊含义:

man (5) crontab:

Percent-signs (%) in the command, unless escaped with backslash (\), 
will be changed into newline characters, and all data after the 
first % will be sent to the command as standard input.

答案2

这并不能直接回答您的问题,但我建议您在 /usr/local/bin(或 ~/bin 或任何合适的位置)中创建一个脚本文件,然后从 cron 调用该文件。这样更容易测试和编辑。

相关内容