写个 crontab ,命令是类似这样的
1
|
/path/to/script
` date
+%Y-%m-%d` |
直接运行很正常,但是在 crotnab 里就出错。
1
2
|
/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 可以看到,
1
|
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。
1
|
/path/to/script
` date
+\%Y-\%m-\%d` |