crontab 里 % 引发的问题
2014-07-25 11:57 briller 阅读(324) 评论(0) 编辑 收藏 举报写个 crontab ,命令是类似这样的
/path/to/script `date +%Y-%m-%d`
直接运行很正常,但是在 crotnab 里就出错。
/bin/sh: -c: line 1: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 2: syntax error: unexpected end of file
google 了好一阵才找到答案。原来 crontab 里的 % 是有特殊意义的,在这里需要转义。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.
% 如果没有用 \ 转义,就会被替换成换行。所以之前的 crontab 就出错了。
解决办法:可以在 % 前面都加个 \ ,对于这个例子,写成 date +\%Y-\%m-\%d。