crontab 执行 Python 脚本失败

编程入门 行业动态 更新时间:2024-10-25 12:22:39
本文介绍了crontab 执行 Python 脚本失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

crontab 无法执行 Python 脚本.我用来运行 Python 脚本的命令行没问题.

crontab fails to execute a Python script. The command line I am using to run the Python script is ok.

这些是我尝试过的解决方案:

main.py顶部添加#!/usr/bin/env pythoncrontab的顶部添加PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/binchmod 777main.py 文件service cron restart add #!/usr/bin/env python at the top of the main.py add PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin at the top of crontab chmod 777 to the main.py file service cron restart

我的 crontab 是:

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

*/1 * * * * python /home/python_prj/main.py

并且/var/log/syslog 中的日志是:

Nov  6 07:08:01 localhost CRON[28146]: (root) CMD (python /home/python_prj/main.py)

没有别的.

main.py脚本调用了python_prj下其他模块的一些方法,有关系吗?

The main.py script calls some methods from other modules under python_prj, does that matter?

有人可以帮我吗?

推荐答案

main.py脚本调用python_prj下其他模块的一些方法,有关系吗?

The main.py script calls some methods from other modules under python_prj, does that matter?

是的,确实如此.所有模块都需要在运行时可找到.您可以通过多种方式完成此操作,但最合适的方法可能是在您的 crontab 中设置 PYTHONPATH 变量.

Yes, it does. All modules need to be findable at run time. You can accomplish this in several ways, but the most appropriate might be to set the PYTHONPATH variable in your crontab.

您可能还想在 crontab 中设置 MAILTO 变量,以便您收到带有任何追溯的电子邮件.

You might also want to set the MAILTO variable in crontab so you get emails with any tracebacks.

[更新] 这是我的 crontab 的顶部:

[update] here is the top of my crontab:

www:~# crontab -l

DJANGO_SETTINGS_MODULE=djangocron.settings
PATH=...
PYTHONPATH=/home/django
MAILTO="cron-notices@example"
...
# m h  dom mon dow   command
10-50/10 * * * *               /home/django/cleanup_actions.py
...

(每 10 分钟运行一次清理操作,除了在整点结束时).

(running cleanup actions every 10 minutes, except at the top of the hour).

这篇关于crontab 执行 Python 脚本失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 04:56:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/678954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   crontab   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!