crontab条目包含%号问题

简介: crontab条目中包含%号,最常见的取时间,如:date +%d, 对%需要使用\进行转义,否则不能按预期执行,正确做法为: * * * * * echo "`date +\%d`" > /tmp/r1r.
crontab条目中包含%号,最常见的取时间,如:date +%d,
对%需要使用\进行转义,否则不能按预期执行,正确做法为:
* * * * * echo "`date +\%d`" > /tmp/r1r.txt
而不能为
* * * * * echo "`date +%d`" > /tmp/r1r.txt


%是crontab的特殊字符,所有%后的被当作了标准输入,这可以通过“ man 5 crontab”查看到说明:
The entire command portion of the line, up to a newline or a "%" character,
will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.
A "%" character in the command, unless escaped with a backslash (\), 
will be changed into newline char-acters,
and all data after the first % will be sent to the command as standard input.


示例:
$ cat /tmp/hello.txt 
cat: /tmp/hello.txt: 没有那个文件或目录
$ echo -e "`crontab -l`\n* * * * * cat > /tmp/hello.txt % hello word"|crontab -
$ crontab -l|grep hello.txt
* * * * * cat > /tmp/hello.txt % hello word
$ cat /tmp/hello.txt 
 hello word
相关文章
|
12天前
|
弹性计算 运维 Shell
|
25天前
|
搜索推荐 Linux Shell
目录及文件管理、文本内容操作、grep过滤文件内容
目录及文件管理、文本内容操作、grep过滤文件内容
|
9月前
|
Shell Linux
附加到文件
附加到文件
29 1
|
安全 Linux Shell
让我带你一起了解一下 ls -l 命令输出的内容都有哪些意义,以及文件权限如何调整
让我带你一起了解一下 ls -l 命令输出的内容都有哪些意义,以及文件权限如何调整
68 0
Confluence 6 删除一个附加的文件
你需要具有 删除附件(Delete Attachment)的空间权限来删除一个附加的文件。 希望删除一个附加文件的所有版本: 进入含有附件的页面中。Go to  > Attachments选择你希望删除文件的后面的 删除(Delete)按钮。
1354 0