运行cron作业时会在查找匹配的“`”时创建错误的意外EOF

时间:2012-06-23 09:54:33

标签: cron crontab cron-task

我可以看到这是“常见”错误,但在我的情况下无法找到解决方案...

使用以下命令运行Crontab作业:

expr `date +%W` % 2 > /dev/null && curl https://mysite.com/myscript

它会导致错误:

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
你可以帮我解决一下这个问题吗?非常感谢你提前!

2 个答案:

答案 0 :(得分:19)

您必须转义%字符。 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.

答案 1 :(得分:1)

尝试逃避%并且不要使用反引号来填充date - 命令。请用$()

括起来
expr $(date +\%W) % 2 > /dev/null && curl https://mysite.com/myscript

OR

expr $(date +\%W % 2) > /dev/null && curl https://mysite.com/myscript