为什么百分号 (%) 在 crontab 中不起作用? [英] Why percent signs (%) do not work in crontab?

查看:19
本文介绍了为什么百分号 (%) 在 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?

推荐答案

这是一个转义变量的问题:

It is a matter of escaping variables:

* * * * * /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.

Note I used /bin/bash instead of just bash.

这篇关于为什么百分号 (%) 在 crontab 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆