简体   繁体   中英

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?

如果您使用虚拟环境,请使用virtualenv中的python二进制文件

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM