简体   繁体   English

为什么 Python 脚本可以从 CLI 运行,但从 cron 作业调用时却不能运行?

[英]Why does a Python script work from the CLI, but not when called from a cron job?

I have created a Python script that I want to run daily via a cronjob on an Ubuntu server.我创建了一个 Python 脚本,我想通过 Ubuntu 服务器上的 cronjob 每天运行它。

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.从 CLI 调用时,脚本运行良好。
However, when I attempt to run the script via cronjob, the script fails to run properly.但是,当我尝试通过 cronjob 运行脚本时,脚本无法正常运行。 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.我在脚本中添加了 Python 日志记录。 When the script is called from the CLI, the logging happens properly.当从 CLI 调用脚本时,日志记录会正确发生。 When called from cron, python logging fails to write to it's log file.从 cron 调用时,python 日志记录无法写入其日志文件。

I suspect the problem is the cronjob is not running the shell when it requests the script.我怀疑问题是 cronjob 在请求脚本时没有运行 shell。

I can't seem to find anywhere that the cron daemon was logging it's errors.我似乎找不到 cron 守护进程记录错误的任何地方。

I created /var/log/cron.log我创建了/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.由于我认为问题源于 ENV 变量,因此我尝试让 cronjob 为自己显示 ENV。

Here's what my crontab looks like:这是我的 crontab 的样子:
$ crontab -u username -e $ crontab -u 用户名 -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? ENV 变量也会输出到哪里?

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. crontab 可能找不到 python 可执行文件,尽管它可以在 CLI 上找到,因此您需要记下 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. Crontab 为脚本提供了一个环境,该环境与普通用户环境不同。

3 17 * * * /bin/sh python /home/username/public_html/IDM_app/manage.py cleanUpPosts  

this line seems wrong.这条线似乎是错误的。 It seems you're running python as if it was a shell script.看来您正在运行python ,就好像它是一个 shell 脚本一样。

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

相关问题 如果通过cron作业执行python脚本,为什么在从Java获得结果之前完成python脚本? - Why does the python script finish before it gets the results from java if executed through cron job? Selenium - Python 脚本可从 CLI 运行,但使用 cron 启动时出错 - Selenium - Python script works from CLI, but errors when launching with cron 使用 MacOS 的 cron 作业执行 python 脚本 - Executing python script with cron job from MacOS 为什么Excel宏在Excel中起作用,但从Python调用时却不能起作用? - Why does Excel macro work in Excel but not when called from Python? 从Mac Cron作业而不是从Mac Terminal运行时,Python脚本错误出 - Python script errors out when run from mac cron job but not from mac terminal python 脚本不能从 cron 运行 - python script does not run from cron 从命令行运行Python脚本时,MySQLdb模块可以工作,但不能作为cron作业 - MySQLdb module works when running Python script from the command line, but not as a cron job 为什么我的 python 脚本在 temal 和 cron 之间的工作方式不同? - Why does my python script work differently between the teminal and cron? 来自Cron的Python脚本 - Python script from Cron 为什么从PHP调用的长时间运行的Python脚本失败 - Why does a long running Python script called from PHP fail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM