How to run a Django script using crontab
Entry updated March 6, 2008 at 6:34 a.m.
In the midst of building a Django app with a feed aggregation component, I ran into what became a major headache: How to run a Python script inside a Django app using a crontab.
Setting up the basic crontab was easy despite my lack of experience in this area. The problems centered on cron not having the correct PYTHONPATH to load modules such as feedparser or Django. I would receive error after error saying "could not import X module." More frustrating was the fact that the script ran fine from the command line.
I solved the issue -- big props to Mock for his help -- by using a crontab to run the Python script via a Bash script that sourced ".bash_profile". That file exported the variables for my PYTHONPATH and DJANGO_SETTINGS_MODULE. (See Jeff Croft's instructions on setting up Django on Dreamhost for more information.)
Here's the crontab (with fake paths):
MAILTO="myaddress@email.com"
1 * * * * /home/usr/django/django_projects/myproject/feed_updater
And here's the Bash script (with fake paths):
#!/bin/bash
. ~/.bash_profile
/usr/bin/python /home/user/django/django_projects/myproject/apps/aggregator/bin/update_feeds.py
(The feed aggregator I'm using is nearly the same as the one used by the Django Community app.)
I ran across many solutions to this problem in my research to find out what the heck was going on. Here are some the the best ones:
- Webfaction forum on Django and cron
- Django and Crontab: Best friends
- Making Django environmentally friendly
- Standalone Django scripts
You can also read through the Google Groups thread I started for more tips on running Django scripts via cron.
If there is a better way of running this script using cron, or another method entirely, please let me know by posting a comment.
Five comments
Another way to run a crontab for django is to import the setup_environ() function.
So if the python script that I want to execute is in the root project folder, I place the following at the start of the script file:
And the in the crontab, all you need to do is put a single entry for that file.
I use to create a urlpattern which has a standard template, short view to run all the commands I need. It's just like a tiny app without model.
Then the cron is setup to command:
This will run everything I need when I need it, overcoming the pythonpath problems.
It might be a good idea to make that URLpattern secret and hard to guess, if it's a heavy load.
It allows me to run the scripts whenever I need outside the scheduled CRON times simply by accessing that URL.
Frank Malina, web developer
That's all much to complicated. In you Django script, just do:
And then the normal code and imports from your project. You can run that file with
Thanks for posting the additional info Humphrey, Frank and Julian!
I wrote this entry when this site was hosted on Dreamhost, which isn't the best environment for Django apps. I believe it also presented a different challenge in terms of running cron scripts that needed to load the Django environment (settings) to perform the necessary action.
I personally prefer to just create a custom management command. This makes it real simple and flexible. Nice post.
Post a comment
Please use Markdown syntax for formatting. No HTML is allowed. By using this comment form, it's assumed that you agree with the terms of my comment policy.