How can I run a django management command by cron job

前端 未结 2 1943
太阳男子
太阳男子 2021-01-01 00:23

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

相关标签:
2条回答
  • 2021-01-01 01:15

    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
    
    0 讨论(0)
  • 2021-01-01 01:17

    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
    
    0 讨论(0)
提交回复
热议问题