为什么百分号(%)没有在crontab中工作? [英] Why percent signs (%) do not work in crontab?
本文介绍了为什么百分号(%)没有在crontab中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写文件到一个日志运行使用cron的bash脚本。在cron中的调用看起来是这样的:
I'm writing files out to a log ran by a bash script using cron. The call on cron looks like this:
*/25 * * * * bash script.sh > "/var/log/$(date +%Y-%m-%d_%H:%M).log"
但是,当我检查记录以crontab
But when I check the crontab it records as
*/25 * * * * bash script.sh > "/var/log/$(date +).log"
和它永远不会写入日志文件。有什么我需要改变让cron来写日期?
And it never writes the log file. Is there something I need to change to get cron to write the date?
推荐答案
这是逃避变量的问题:
* * * * * /usr/bin/touch /tmp/$(date +\%Y:\%m).log
# ^ ^
工作给我。
从男子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"
# ^ ^ ^ ^ ^
应该工作。
<子>注意我用 /斌/庆典
,而不是仅仅庆典
。
Note I used /bin/bash
instead of just bash
.
这篇关于为什么百分号(%)没有在crontab中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文