问题描述
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 python
在crontab
的顶部添加PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
chmod 777
到 main.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:关键词]
发布评论