简体   繁体   English

通过crontab执行python脚本

[英]python script execution through crontab

what happens to my script in python that does not run through crontab every minute. 我的python脚本每分钟不会通过crontab运行时会发生什么情况。 My script has execute permissions and then calls two other scripts in python. 我的脚本具有执行权限,然后在python中调用其他两个脚本。


This is the content of my crontab (#crontab -l):
*/1 * * * * /usr/bin/rsm/samplesMonitor.py
Thank you guys. 感谢大伙们。

Check /var/log/syslog for errors. 检查/var/log/syslog中的错误。

DIAGNOSTICS
       cron requires that each entry in a crontab end in a 
       newline character. If the last entry in a crontab is 
       missing a newline (ie, terminated by  EOF),  cron  will 
       consider the crontab (at least partially) broken. A 
       warning will be written to syslog.

Update: According to your log message, the script is running but returning an error code. 更新:根据您的日志消息,脚本正在运行,但返回错误代码。 Cron will email you the output, if you have a mail agent installed. 如果您安装了邮件代理,Cron将通过电子邮件将输出结果发送给您。

Try either: 尝试以下任一方法:

  1. install a mail agent, such as: apt-get install exim4 安装邮件代理,例如: apt-get install exim4
  2. change your cron line to log to file, like so: 更改您的cron行以记录到文件,如下所示:

     * * * * * /usr/bin/rsm/samplesMonitor.py 2>&1 >> /tmp/script.log 

Update 2: I re-read your question and it acurred to me that maybe you are running into python import issues. 更新2:我重新阅读了您的问题,这让我想到您可能正在遇到python导入问题。 You say that your script calls two other scripts. 您说您的脚本调用了另外两个脚本。 My suggestion would be to test running your script from / . 我的建议是从/测试运行脚本。 Python has a default behavior to find imports in the current working directory, so make sure your script can run from any path location. Python具有默认行为,可在当前工作目录中找到导入,因此请确保您的脚本可以在任何路径位置运行。

In the crontab, you can set the starting working directory by calling your script from within another shell process. 在crontab中,可以通过从另一个Shell进程中调用脚本来设置启动工作目录。 For example: 例如:

bash -c "cd THE_WORKING_DIR;/usr/bin/rsm/samplesMonitor.py"

我相信应该是*/1 ,而不是*\\1

It should be */1 instead of *\\1 (forward slash instead of backslash). 它应该是*/1而不是*\\1 (正斜杠而不是反斜杠)。 Also, make sure the path is correct; 另外,请确保路径正确。 there usually are no subdirectories under /usr/bin. / usr / bin下通常没有子目录。

如果您希望它每分钟运行一次,请执行此操作

* * * * * /usr/bin/rsm/samplesMonitor.py

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

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