Unix 中的 Cron 工作

发布于 2024-09-25 23:43:00 字数 804 浏览 1 评论 0原文

我制作了一个脚本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

祁梦 2024-10-02 23:43:00

尝试替换

if [[ $1 = "cron" ]] ; then

if [[ "$1" = "cron" ]] ; then

try to replace

if [[ $1 = "cron" ]] ; then

with

if [[ "$1" = "cron" ]] ; then
轮廓§ 2024-10-02 23:43:00

其他的可读性:您可以说它不同,而不是每 5 分钟调用一次:

 bevore: 10,15,20,25,30,35,40,45,50,55 * * * * "/home/ehimkak/cro...
 after:  */5 * * * *  "/home/ehimkak/cro...

*/5 将每 5 分钟调用一次 cronjob,这使得它更具可读性!
内切

something else for readability: instead of calling every 5minutes you can say it diffrent:

 bevore: 10,15,20,25,30,35,40,45,50,55 * * * * "/home/ehimkak/cro...
 after:  */5 * * * *  "/home/ehimkak/cro...

*/5 will call the cronjob every 5th minute, that makes it more readable!
endo

阳光①夏 2024-10-02 23:43:00

我能够解决这个问题,而且解决方案非常简单。

我刚刚编辑了第一行的 shell 行
现在第一行是

#!/usr/bin/sh

我添加的!我的问题解决了

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

#!/usr/bin/sh

so I added ! and my problem was solved

夏雨凉 2024-10-02 23:43:00

形成一个典型的 crontab(5) 联机帮助页...

“第六”字段(该行的其余部分)指定
要运行的命令。整个命令部分
行,直到换行符或 % 字符,将由以下命令执行
/bin/sh 或由 SHELL 变量中指定的 shell
cronfile。命令中的百分号 (%),除非
用反斜杠()转义,会变成换行符
字符,第一个 % 之后的所有数据将被发送到
命令作为标准输入。

我会给你两个建议:

  • 首先尝试了解正在发生的事情。日志永远是你的朋友。也就是说,在您的脚本中添加如下内容:

    回显$#> /tmp/日志

    回显$@>> /tmp/日志

    回显$*>> /tmp/log

并检查参数是否正在传递。

另一个技巧是尝试传递所有转义的内容,例如:

10,15,20,25,30,35,40,45,50,55 * * * * "/home/ehimkak/cronTabTest/helloworld.sh cron" >>/home/ehimkak/cronTabTest/t1.txt

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:

10,15,20,25,30,35,40,45,50,55 * * * * "/home/ehimkak/cronTabTest/helloworld.sh cron" >>/home/ehimkak/cronTabTest/t1.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文