Unix 中的 Cron 工作
我制作了一个脚本 helloworld.sh
,其路径为 /home/ehimkak/cronTabTest
:
#/usr/bin/sh
echo $1
if [[ $1 = "cron" ]] ; then
echo "hiiiii"
else
echo "sorry"
fi
如果我使用命令从 /
位置运行它
/home/ehimkak/cronTabTest/helloworld.sh cron
它运行良好。
现在我添加了一个 cron 作业,首先将编辑器设置为 vi (export EDITOR=vi),然后使用命令 crontab -e
。
我在那里添加了一行
10,15,20,25,30,35,40,45,50,55 * * * * /home/ehimkak/cronTabTest/helloworld.sh cron>>/home/ehimkak/cronTabTest/t1.txt
结果是脚本正在运行,但输出不符合预期。
我得到的 t1.txt 文件中的输出是,
cron
sorry
但我的输出必须
cron
hiiii
是脚本中没有问题,但我不明白为什么 crontab
会以这种方式运行。
请帮忙...
i have made a script helloworld.sh
and its path is /home/ehimkak/cronTabTest
:
#/usr/bin/sh
echo $1
if [[ $1 = "cron" ]] ; then
echo "hiiiii"
else
echo "sorry"
fi
If I run it from the /
location with command
/home/ehimkak/cronTabTest/helloworld.sh cron
it runs fine.
Now I added a cron job by first setting the editor as vi (export EDITOR=vi) and then used command crontab -e
.
There I added a line
10,15,20,25,30,35,40,45,50,55 * * * * /home/ehimkak/cronTabTest/helloworld.sh cron>>/home/ehimkak/cronTabTest/t1.txt
The result is that the script is running, but the output is not as desired.
The output in the t1.txt file I get is
cron
sorry
but my output must come
cron
hiiii
There is no problem in the script, but i don't understand why crontab
is behaving in such a way.
Please help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试替换
为
try to replace
with
其他的可读性:您可以说它不同,而不是每 5 分钟调用一次:
*/5 将每 5 分钟调用一次 cronjob,这使得它更具可读性!
内切
something else for readability: instead of calling every 5minutes you can say it diffrent:
*/5 will call the cronjob every 5th minute, that makes it more readable!
endo
我能够解决这个问题,而且解决方案非常简单。
我刚刚编辑了第一行的 shell 行
现在第一行是
我添加的!我的问题解决了
I was able to solve the problem and the solution was very simple.
I just edited the shell line that is the first line
now the first line is
so I added ! and my problem was solved
形成一个典型的 crontab(5) 联机帮助页...
“第六”字段(该行的其余部分)指定
要运行的命令。整个命令部分
行,直到换行符或 % 字符,将由以下命令执行
/bin/sh 或由 SHELL 变量中指定的 shell
cronfile。命令中的百分号 (%),除非
用反斜杠()转义,会变成换行符
字符,第一个 % 之后的所有数据将被发送到
命令作为标准输入。
我会给你两个建议:
首先尝试了解正在发生的事情。日志永远是你的朋友。也就是说,在您的脚本中添加如下内容:
回显$#> /tmp/日志
回显$@>> /tmp/日志
回显$*>> /tmp/log
并检查参数是否正在传递。
另一个技巧是尝试传递所有转义的内容,例如:
Form a typical crontab(5) manpage ....
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 % character, will be executed by
/bin/sh or by the shell specified in the SHELL variable of
the cronfile. 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.
i would give you two advices:
first try to understand what is happening. Log is always your friend. Said that, in your script add something like:
echo $# > /tmp/log
echo $@ >> /tmp/log
echo $* >> /tmp/log
And check if the parameters are being passed.
Another tip is try to pass everything escaped like: