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.
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 cronjob is not running the shell when it requests the script.开发者_StackOverflow社区
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 -em 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.
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.
精彩评论