+-
linux – 为什么百分号(%)在crontab中不起作用?
参见英文答案 > How is % special in crontab?                                    1个
我正在使用cron将文件写入由bash脚本运行的日志中.对cron的调用如下所示:

*/25 * * * * bash script.sh > "/var/log/$(date +%Y-%m-%d_%H:%M).log"

但当我检查crontab时,它记录为

*/25 * * * * bash script.sh > "/var/log/$(date +).log"

它永远不会写日志文件.有什么我需要改变让cron写日期吗?

最佳答案
这是一个逃避变量的问题:

* * * * * /usr/bin/touch /tmp/$(date +\%Y:\%m).log
#                                      ^   ^

对我有用.

来自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.

所以

*/25 * * * * /bin/bash script.sh > "/var/log/$(date +\%Y-\%m-\%d_\%H:\%M).log"
#                                                    ^    ^   ^   ^   ^

应该管用.

注意我使用/ bin / bash而不是bash.

点击查看更多相关文章

转载注明原文:linux – 为什么百分号(%)在crontab中不起作用? - 乐贴网