开发者

Running a python script for django via cron

开发者 https://www.devze.com 2023-01-11 07:05 出处:网络
I have a python script that connects to an external host, fetc开发者_如何学Gohes some data, and populates a Django database with the data. The python script that populates the database uses these line

I have a python script that connects to an external host, fetc开发者_如何学Gohes some data, and populates a Django database with the data. The python script that populates the database uses these lines to setup the django environment:

path = os.path.normpath(os.path.join(os.getcwd(), '..'))
sys.path.append(path)
from django.core.management import setup_environ
import settings
setup_environ(settings)

Then I have a shell script that actually runs the python script:

export PYTHONPATH=$PYTHONPATH:/home/django/project_dir/
cd ~/project_dir/scripts/
~/virtualenv/bin/python my_script.py

And then my cron config, which is placed in /etc/cron.d/

0 1 * * * django ~/project_dir/scripts/my_script.sh > /var/log/django_cron.log

Note that the django project has it's own user and virtual environment.

The shell script runs fine when I am logged in as the django user. But the cron won't run! I get no errors in the log file. I'm sure it's something very simple, but I just don't see it...


The problem was actually that I have an environment variable to tell django if it is running in development or production. Crontab doesn't execute the .bashrc file where I export this variable so that had to be added to my shell script:

export FLAVOR=live
export PYTHONPATH=$PYTHONPATH:/home/django/project_dir/
cd ~/project_dir/scripts/
~/virtualenv/bin/python my_script.py


Two things come to mind. I don't think cron will make the tilde substitution you expect -- perhaps I'm wrong, but try specifying the full path (i.e. /home/myuser/project_dir/...) in your cron entry as well as your scripts.

Also, what's the "django" in your cron entry? Is it the name of your script or is that a typo? Finally, to log possible errors from the execution of your script, try:

0 1 * * * /home/myuser/project/script.sh > /var/log/django_cron.log 2>&1
0

精彩评论

暂无评论...
验证码 换一张
取 消