user1592380
user1592380

Reputation: 36267

How can I run a django management command by cron job

I am working with a django app called django-mailbox. The purpose of this is to import email messages via pop3 and other protocols and store them in a db. I want to do this at regular intervals via a chron. In the documentation http://django-mailbox.readthedocs.org/en/latest/topics/polling.html it states:

Using a cron job

You can easily consume incoming mail by running the management command named getmail (optionally with an argument of the name of the mailbox you’d like to get the mail for).:

python manage.py getmail

Now I can run this at the command line locally and it works but if this was deployed to an outside server which was only accessible by a URL how would this command be given?

Upvotes: 11

Views: 13784

Answers (2)

Chris Montanaro
Chris Montanaro

Reputation: 18202

If you are using a virtual env use the python binary from the virtualenv

* * * * * /path/to/virtualenv/bin/python /path/to/project/manage.py management_command

Upvotes: 23

Guy Gavriely
Guy Gavriely

Reputation: 11396

on the server machine:

$ sudo crontab -l
no crontab for root
$ sudo crontab -e
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

Choose 1-4 [2]:    

choose your preferred editor

then see http://en.wikipedia.org/wiki/Cron for how to schedule when will the command run, direct it to some .sh file on your machine, make sure you give full path as this is going to run in root user context.

the script the cron will run may look something like:

#!/bin/bash
cd /absolute/path/to/django/project
/usr/bin/python ./manage.py getmail

Upvotes: 3

Related Questions