Question

I have custom commend:

python manage.py checksomething

How to run this every two hours?

I saw django-cron:

#example from doc
from django_cron import cronScheduler, Job

from MyMailFunctions import check_feedback_mailbox

class CheckMail(Job):
        # run every 300 seconds (5 minutes)
        run_every = 300

        def job(self):
                # This will be executed every 5 minutes
                check_feedback_mailbox()

cronScheduler.register(CheckMail)

but how to run it? It will turn on automatically? Do I have something set up on the server? Where should be this file? (In what folder?)

Was it helpful?

Solution

Use crontab, its very simple to use

To edit your crontab:

$ crontab -e

and add your job:

* */2 * * * python /path/to/project/manage.py checksomething

OTHER TIPS

The idea with the django_cron is to set a job that will run every X seconds within your project.

You create the job (CheckMail function in your example) and set its period (run_every = 300 which means every 300 seconds = 5 minutes).

After setting the job you register the job using cronScheduler.register(CheckMail).

To make the job run every 5 minutes you need in your url.py to add the following :

import django_cron
django_cron.autodiscover()

Doing that will make the django_cron active and your job will start every 5 minutes.

This is a better approch then calling manage.py checksomthing from a script. that way your django project will run its own job within your django project and will not be depend on a running script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top