Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a django management command by cron job

Tags:

python

django

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?

like image 385
user1592380 Avatar asked Nov 14 '13 22:11

user1592380


People also ask

How do I run a cron job in django?

Steps to setup a cron job in Django Create a file anywhere within your django's project according to your module directory structure for e.g. myapp/cron.py and define the function which you want to be executed automatically via cron. This will be your cron job. We define a cron just as above.

Can cron run commands?

Cron allows Linux and Unix users to run commands or scripts at a given date and time. You can schedule scripts to be executed periodically. Cron is one of the most useful tool in a Linux or UNIX like operating systems. It is usually used for sysadmin jobs such as backups or cleaning /tmp/ directories and more.

Does django crontab work windows?

Using django-cron is not the case and cannot be used on Windows, because Windows do not support cron job scheduling. You can use Windows analog of the Unix cron command called "schtasks" to schedule execution of your script or Windows Task Scheduler.


1 Answers

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
like image 94
Chris Montanaro Avatar answered Sep 20 '22 16:09

Chris Montanaro