我有一个关于 curl
的 cron 问题:
curl -w "%{time_total}\n" -o /dev/null -s http://myurl.com >> ~/log
效果很好,在日志文件中添加一行 total_time。
但是与 cron 相同的一行没有做任何事情。
这不是路径问题,因为 curl http://myurl.com >> ~/log
有效。
最佳答案
%
是crontab
的特殊字符。来自 man 5 crontab
:
The "sixth" field (the rest of the line) specifies the command to be run. 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 characters, and all data after the first % will be sent to the command as standard input.
所以你需要转义%
字符:
curl -w "%{time_total}\n" -o /dev/null -s http://myurl.com >> ~/log
到
curl -w "\%{time_total}\n" -o /dev/null -s http://myurl.com >> ~/log
^
关于bash - 百分号 % 在 crontab 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27123367/