Crontab无法执行Python脚本 [英] Crontab fails to execute Python script

查看:670
本文介绍了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的顶部添加#!/ usr / bin / env python .py

  • 添加 PATH = / usr / bin:/ bin:/ usr / sbin:/ sbin:/ usr / local / bin crontab
  • 的顶部
  • chmod 777 main.py 文件

  • 服务cron重新启动

  • 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?

任何人都可以帮助我吗?

Anyone can help me?

推荐答案


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.com"
...
# 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆