擅长:python、mysql、java
<p>crontab失败的另一个原因是:对<code>%</code>字符的特殊处理。</p>
<p>从<a href="http://man7.org/linux/man-pages/man5/crontab.5.html" rel="noreferrer">man file</a>:</p>
<pre><code>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.
</code></pre>
<p>在我的特殊情况下,我使用<code>date --date="7 days ago" "+%Y-%m-%d"</code>来生成脚本的参数,但它却在无声地失败。当我检查<code>syslog</code>并看到命令在<code>%</code>符号处被截断时,我终于发现发生了什么。你需要这样逃避:</p>
<pre><code>date --date="7 days ago" "+\%Y-\%m-\%d"
</code></pre>
<p>有关详细信息,请参见此处:</p>
<p><a href="http://www.ducea.com/2008/11/12/using-the-character-in-crontab-entries/" rel="noreferrer">http://www.ducea.com/2008/11/12/using-the-character-in-crontab-entries/</a></p>