简体   繁体   English

如何通过cron job运行django管理命令

[英]How can I run a django management command by cron job

I am working with a django app called django-mailbox. 我正在使用一个名为django-mailbox的django应用程序。 The purpose of this is to import email messages via pop3 and other protocols and store them in a db. 这样做的目的是通过pop3和其他协议导入电子邮件消息并将其存储在数据库中。 I want to do this at regular intervals via a chron. 我想通过一个chron定期做这件事。 In the documentation http://django-mailbox.readthedocs.org/en/latest/topics/polling.html it states: http://django-mailbox.readthedocs.org/en/latest/topics/polling.html文档中,它指出:

Using a cron job 使用cron作业

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).: 您可以通过运行名为getmail的管理命令轻松地使用传入邮件(可选地使用您要获取邮件的邮箱名称的参数):

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? 现在我可以在命令行本地运行它并且它可以正常工作,但是如果将它部署到只能通过URL访问的外部服务器,那么该命令将如何给出?

如果您使用虚拟环境,请使用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. 然后查看http://en.wikipedia.org/wiki/Cron ,了解如何安排命令运行的时间,将其指向计算机上的某个.sh文件,确保提供完整路径,因为这将在root中运行用户上下文。

the script the cron will run may look something like: cron将运行的脚本可能类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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