为什么Python脚本从CLI工作,但不是从cron作业调用时? [英] Why does a Python script work from the CLI, but not when called from a cron job?

查看:139
本文介绍了为什么Python脚本从CLI工作,但不是从cron作业调用时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Python脚本,我想在Ubuntu服务器上通过cronjob每天运行。



这是如何从命令行运行脚本:

  python /home/username/public_html/IDM_app/manage.py cleanUpPosts 

从CLI调用时,脚本运行正常。

然而,当我尝试通过cronjob运行脚本时,脚本无法正常运行。
日志文件显示正在请求脚本,但它们没有显示为什么脚本没有正常执行。



我将Python日志记录添加到脚本。当从CLI调用脚本时,日志记录正常发生。当从cron调用时,python日志无法写入它的日志文件。



我怀疑问题是cronjob在请求脚本时没有运行shell。 / p>

我似乎找不到cron守护程序记录它的错误的任何地方。



我创建了/var/log/cron.log

但是似乎没有更新。



由于我认为问题是来自ENV变量,我试图让cronjob显示ENV本身。



这是我的crontab看起来像:

$ crontab -u用户名-e

  mh dom命令
43 17 * * * / bin / sh python /home/username/public_html/IDM_app/manage.py cleanUpPosts
43 17 * * * python /home/username/public_html/IDM_app/manage.py cleanUpPosts
45 21 * * * echo----------------- echoimport os; print os.environ| python
47 21 * * * / bin / sh echo------带shell -------; echoimport os; printos.environ| python
MAILTO=bryanw@nowhere.com

我以为会输出到屏幕,但它没有。 ENV变量在哪里输出?



无论如何,这里是日志文件:

  n 5 /var/log/*.log 
==> /var/log/auth.log< ==
Jan 13 17:43:01 servername CRON [7901]:pam_unix(cron:session):为用户用户名打开的会话(uid = 0)
Jan 13 17:43:01 servername CRON [7902]:pam_unix(cron:session):为用户用户名打开的会话(uid = 0)
Jan 13 17:44:48 servername su [7909] su为root用户名
Jan 13 17:44:48 servername su [7909]:+ / dev / pts / 0用户名:root
Jan 13 17:44:48 servername su [7909]:pam_unix (su:session):用户以root用户打开的会话(uid = 1000)

==> /var/log/bootstrap.log< ==

==> /var/log/cron.log< ==

==> /var/log/daemon.log< ==

==> /var/log/dpkg.log< ==

==> /var/log/kern.log< ==

==> /var/log/lpr.log< ==

==> /var/log/mail.log< ==

==> /var/log/mysql.log< ==

==> /var/log/pycentral.log< ==

==> /var/log/user.log< ==

我还应该尝试什么,为什么我的脚本不能正常运行?

解决方案

crontab可能找不到python可执行文件,你需要写下python的完整路径。同样来自

  which python 

Crontab为脚本提供了一个环境,这与正常用户环境不同。


I have created a Python script that I want to run daily via a cronjob on an Ubuntu server.

This is how this script would be run from the Command Line:

python  /home/username/public_html/IDM_app/manage.py cleanUpPosts  

When called from the CLI, the script works fine.
However, when I attempt to run the script via cronjob, the script fails to run properly. The log files show that the script is being requested, but they are not showing why the script is not executing properly.

I added Python logging to the script. When the script is called from the CLI, the logging happens properly. When called from cron, python logging fails to write to it's log file.

I suspect the problem is the the cronjob is not running the shell when it requests the script.

I can't seem to find anywhere that the cron daemon was logging it's errors.

I created /var/log/cron.log
Yet that doesn't seem to be updating.

Since I think the problem is stemming from the ENV variables, I tried to get the cronjob to display the ENV for itself.

Here's what my crontab looks like:
$ crontab -u username -e

m h  dom mon dow   command  
43 17 * * * /bin/sh python /home/username/public_html/IDM_app/manage.py cleanUpPosts  
43 17 * * * python /home/username/public_html/IDM_app/manage.py cleanUpPosts  
45 21 * * * echo "-----------------"; echo "import os; print os.environ" | python  
47 21 * * * /bin/sh echo "------with shell-------"; echo "import os; printos.environ" | python  
MAILTO=bryanw@nowhere.com  

I thought it would output to the screen, but it didn't. Where would the ENV variables be outputting too?

Regardless, here are the log files:

# tail -n 5 /var/log/*.log   
==> /var/log/auth.log <==  
Jan 13 17:43:01 servername CRON[7901]: pam_unix(cron:session): session opened for user username by (uid=0)  
Jan 13 17:43:01 servername CRON[7902]: pam_unix(cron:session): session opened for user username by (uid=0)  
Jan 13 17:44:48 servername su[7909]: Successful su for root by username  
Jan 13 17:44:48 servername su[7909]: + /dev/pts/0 username:root  
Jan 13 17:44:48 servername su[7909]: pam_unix(su:session): session opened for user root by username(uid=1000)  

==> /var/log/bootstrap.log <==  

==> /var/log/cron.log <==  

==> /var/log/daemon.log <==  

==> /var/log/dpkg.log <==  

==> /var/log/kern.log <==  

==> /var/log/lpr.log <==  

==> /var/log/mail.log <==  

==> /var/log/mysql.log <==  

==> /var/log/pycentral.log <==  

==> /var/log/user.log <==  

What else should I try so I can determine why my scripts aren't running properly?

解决方案

The crontab probably cannot find the python executable although it can on the CLI, so you need to write down the complete path to python. The same you get from

which python

Crontab supplies an environment for the scripts, which is not the same as the normal user environment.

这篇关于为什么Python脚本从CLI工作,但不是从cron作业调用时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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