参见英文答案 > 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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。