简体   繁体   English

从crontab运行时Shell脚本无法正常运行

[英]Shell script doesn't run properly while running from crontab

I read the other related topics but they didn't help me. 我阅读了其他相关主题,但它们并没有帮助我。

I have a shell script which checks if my python script is not running,it will run it. 我有一个shell脚本,它检查我的python脚本是否未运行,它将运行它。 Otherwise it will just skip and do nothing. 否则,它只会跳过而不执行任何操作。

It totally works when I use: bash myshellscrip.sh And I get the result that I want which is doing some tasks and sending emails to some correspondents. 当我使用它时,它完全可以工作:bash myshellscrip.sh而且我得到想要的结果,它正在执行一些任务并将电子邮件发送给某些通讯员。 However, when I try to run this particular shell script on crontab, it doesn't send out the emails and doesn't do the other tasks. 但是,当我尝试在crontab上运行此特定的Shell脚本时,它不会发送电子邮件,也不会执行其他任务。 I tried the following on crontab and none of them worked. 我在crontab上尝试了以下方法,但没有一个起作用。

    * * * * * /bin/bash /path/to/my/script/myshellscrip.sh 
    * * * * * /bin/bash /path/to/my/script/myshellscrip.sh >> /some/other/path/output.txt

When I save the changes into 'output.txt' file, it creates the file but it doesn't send the emails or doing other tasks. 当我将更改保存到“ output.txt”文件中时,它会创建文件,但不会发送电子邮件或执行其他任务。

I also tried the option of reboot because I need this program to run at start up too, and this didn't work: 我还尝试了重新启动的选项,因为我也需要在启动时也运行该程序,但这不起作用:

    @reboot /bin/bash /path/to/my/script/myshellscrip.sh

Does anyone know how to fix it? 有谁知道如何修理它?

EDIT: 编辑:

As I was checking with the simplest shell scrip like: 正如我在检查最简单的shell脚本一样:

    #!/bin/sh
    /usr/bin/python /home/pi/DCA/code.py

My crontab wouldn't have any output in my output.txt file although my code.py have something printing out, too. 我的crontab在output.txt文件中没有任何输出,尽管我的code.py也有打印出来的东西。

However, when I use a very simple python code for example only a 'print' statement it will run and save the output into output.txt. 但是,当我使用非常简单的python代码作为示例时,它将仅运行“ print”语句并将其输出保存到output.txt中。

Seems like your shell script crashes / stops before it can do something (possibly due to the environment being different or permission issues). 似乎您的shell脚本在无法执行某些操作之前便崩溃了/停止了(可能是由于环境不同或权限问题所致)。 You can check /var/log/syslog to find out. 您可以检查/var/log/syslog来查找。

You could try removing /bin/bash , I don't think that's necessary? 您可以尝试删除/bin/bash ,我认为这不是必需的吗?

Run the cron job in debug mode. 在调试模式下运行cron作业。 for that, Add -x to the bash command on the cronjob and save their output in the file. 为此,在cronjob的bash命令中添加-x并将其输出保存在文件中。

bash -x /path/to/script.sh >> /path/to/the/output.txt

You can find the problem. 您可以找到问题所在。

Apparently crontab was running my script several times. 显然crontab在运行我的脚本几次。 So I tried to use different locking mechanisms to put a lock around my scrip but only using flock worked for me. 因此,我尝试使用不同的锁定机制在我的股票上加锁,但只有使用flock才有效。 In my crontab I added this line: 在我的crontab中,我添加了以下行:

* * * * * /usr/bin/flock -n /tmp/ms.lockfile /bin/bash /path/to/my/script/myShellScript.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM