by Daniel Davies

Websites, as most programmers will know, are quite different to regular software programs. The main reason is that the "program" only ever runs when a user wants something (makes a request). HTTP, the protocol through which websites operate, has no state. This can be tricky when a large amount of processing is required on a page, because effectively a visitor must wait until all this processing has been done before they view the page. Relying on visitors to the site to trigger mass processing events is a poor way to develop applications.

A preferred method is to have script run in the background, far away from HTTP actually on the server. Django provides an excellent method of running backend processing scripts via the manage.py file. Unfortunately, its documentation is somewhat lacking and will only get you so far. Assuming that you have read the documentation and are comfortable with having manage.py execute custom functions, there's one final stage to get the job running on a scheduled basis

You need to make sure that your cron has the correct pythonpath, else Django will not be able to locate your settings.py file and load your Django environment. Above your Cron, simple add two lines.

PYTHONPATH=${PYTHONPATH}:<path-to-project>
DJANGO_SETTINGS_MODULE=<project-name>.settings

This cron will now run every 15 minutes with your custom management command! Here's a more real example of how I parse all the feed going in to my "Life Feed"

PYTHONPATH=${PYTHONPATH}:~/public_html/danux/
DJANGO_SETTINGS_MODULE=danux.settings
*/15 * * * * python ~/public_html/danux/danux/manage.py lifefeed &> /dev/null

Talk about The Missing Link - Running Django's manage.py on a Cron

No comments yet! Why not be the first?

Contact Daniel…

Request a Quote