通过crontab执行python脚本

发布于 2024-10-19 21:28:46 字数 221 浏览 4 评论 0原文

我的 python 脚本不会每分钟都通过 crontab 运行,会发生什么情况。 我的脚本具有执行权限,然后调用 python 中的另外两个脚本。


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

what happens to my script in python that does not run through crontab every minute.
My script has execute permissions and then calls two other scripts in python.


This is the content of my crontab (#crontab -l):
*/1 * * * * /usr/bin/rsm/samplesMonitor.py


Thank you guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

赤濁 2024-10-26 21:28:46

检查 /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.

更新:根据您的日志消息,脚本正在运行,但返回错误代码。如果您安装了邮件代理,Cron 会将输出通过电子邮件发送给您。

尝试以下任一方法:

  1. 安装邮件代理,例如:apt-get install exim4
  2. 更改您的 cron 行以记录到文件,如下所示:

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

更新 2: 我重新阅读了您的问题,我发现您可能遇到了 python 导入问题。你说你的脚本调用了另外两个脚本。我的建议是测试从 / 运行脚本。 Python 的默认行为是在当前工作目录中查找导入,因此请确保您的脚本可以从任何路径位置运行。

在 crontab 中,您可以通过从另一个 shell 进程中调用脚本来设置起始工作目录。例如:

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

Check /var/log/syslog for errors.

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.

Try either:

  1. install a mail agent, such as: apt-get install exim4
  2. change your cron line to log to file, like so:

    * * * * * /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. 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.

In the crontab, you can set the starting working directory by calling your script from within another shell process. For example:

bash -c "cd THE_WORKING_DIR;/usr/bin/rsm/samplesMonitor.py"
絕版丫頭 2024-10-26 21:28:46

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

I believe it should be */1, not *\1.

吹泡泡o 2024-10-26 21:28:46

它应该是 */1 而不是 *\1 (正斜杠而不是反斜杠)。另外,请确保路径正确; /usr/bin 下通常没有子目录。

It should be */1 instead of *\1 (forward slash instead of backslash). Also, make sure the path is correct; there usually are no subdirectories under /usr/bin.

囚我心虐我身 2024-10-26 21:28:46

如果你想让它每分钟运行一次,就这样做

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

If you want it to run every minute, just do this

* * * * * /usr/bin/rsm/samplesMonitor.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文