Cron在单撇号上失败 [英] Cron fails on single apostrophe
问题描述
以下操作符合预期:
date +'%d-%b-%Y-%H-%M'
28-Sep-2009-14-28
但是crontab中没有以下4个条目正在工作。
But none of the following 4 entries from crontab are working.
* * * * * date +\'%d-%b-%Y-%H-%M\' >> /backup/shantanu/testing.txt
* * * * * date +'%d-%b-%Y-%H-%M' >> /backup/shantanu/testing1.txt
* * * * * date +"%d-%b-%Y-%H-%M" >> /backup/shantanu/testing2.txt
* * * * * date +\"%d-%b-%Y-%H-%M\" >> /backup/shantanu/testing3.txt
Error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I can save the same code in a shell script and set the cron, but I will like to know if it is possible to directly set a cron for the task.
我想要设置的实际cron项目看起来像这样...
The actual cron entry that I am trying to set looks something like this...
16 * * * * mysqldump myDB myTB > /backup/ABCbc$(date +'%d-%b-%Y-%H-%M').sql 2> /backup/ABCbc_errORS$(date +'%d-%b-%Y-%H-%M').txt
推荐答案
作业命令与直接输入到交互式shell中的命令的行为不同:
There are four common causes for cron job commands to behave differently compared to commands typed directly into an interactive shell:
- Cron提供有限的环境, $ c> $ PATH 以及其他预期变量。
- Cron通过调用
/ bin / sh
- Cron特别处理
%
字符(它被转换为换行符
- Cron provides a limited environment, e.g., a minimal
$PATH
, and other expected variables missing. - Cron invokes
/bin/sh
by default, whereas you may be using some other shell interactively. - Cron treats the
%
character specially (it is turned into a newline in the command). - The command may behave differently because it doesn't have a terminal available.
您必须先在crontab文件中的%
字符前加上 \
%
,例如
You must precede all %
characters with a \
in a crontab file, which tells cron to just put a %
in the command, e.g.
16 * * * * mysqldump myDB myTB > "/backup/ABCbc$(date +'\%d-\%b-\%Y-\%H-\%M').sql" 2> "/backup/ABCbc_errORS$(date +'\%d-\%b-\%Y-\%H-\%M').txt"
(另一方面,请务必在$ variable_substitution
或(命令替换)
,除非你知道为什么不在特定情况下做。否则,如果变量contents或命令输出包含空格或?* \ [
,它们将被shell解释。)
(As a separate matter, always put double quotes around a "$variable_substitution"
or a "$(command substitution)"
, unless you know why not do it in a particular case. Otherwise, if the variable contents or command output contains whitespace or ?*\[
, they will be interpreted by the shell.)
这篇关于Cron在单撇号上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!