简体   繁体   English

Crontab无法执行Python脚本

[英]Crontab fails to execute Python script

crontab fails to execute a Python script. crontab无法执行Python脚本。 The command line I am using to run the Python script is ok. 我用来运行Python脚本的命令行是可以的。

These are solutions I had tried: 这些是我尝试的解决方案:

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

my crontab is: 我的crontab是:

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

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

and the log in /var/log/syslog is: 并且/ var / log / syslog中的日志是:

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

and nothing else. 没有别的。

The main.py script calls some methods from other modules under python_prj , does that matter? main.py脚本从python_prj下的其他模块调用一些方法,这有关系吗?

Anyone can help me? 有人可以帮帮我吗?

The main.py script calls some methods from other modules under python_prj, does that matter? main.py脚本从python_prj下的其他模块调用一些方法,这有关系吗?

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中设置PYTHONPATH变量。

You might also want to set the MAILTO variable in crontab so you get emails with any tracebacks. 您可能还想在crontab中设置MAILTO变量,以便收到任何回溯的电子邮件。

[update] here is the top of my crontab: [更新]这里是我的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
...

(running cleanup actions every 10 minutes, except at the top of the hour). (每10分钟运行一次清理操作,小时除外)。

Any file access in your scripts? 脚本中的任何文件访问权限? And if so, have you used relative paths (or even: no explicit path) in your script? 如果是这样,您是否在脚本中使用了相对路径(甚至:没有显式路径)?
When run from commandline, the actual folder is 'your path', where you start the script from. 从命令行运行时,实际文件夹是“您的路径”​​,从中启动脚本。 When run by cron, 'your path' may be different depending on environment variables. 当由cron运行时,“您的路径”​​可能会因环境变量而异。
So try using absolute paths to any files you access. 因此,请尝试使用您访问的任何文件的绝对路径。

Check the permissions of the script. 检查脚本的权限。 Make sure that it's executable by cron-- try chmod +x main.py. 确保它可以通过cron执行 - 尝试chmod + x main.py.

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

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