How to setup Django cron jobs?

One of my project in Django required cron jobs and as i was new to Python world at that time so after lots of saerch i found this(Django-crontab) elegant and super simple way to do this.

Actually all you have to do is to follow some basic and simple step and at the end of these steps you have cron job setup right from your Django project even without touch or access anything at the crontab end.

So steps are as follows:

  • You have to install this module and you can do this using pip:
pip install django-crontab

 

  • Next step is to add djngo-crontab to Django’s installed apps list in settings.py file:
INSTALLED_APPS = (
    'django_crontab',
    ...
)

 

  • Now next step is to create a new method that should be executed by cron every 5 minutes, i.e. in myproject/cron.py:
def my_scheduled_job():
   #your functionality goes right here

 

  • Now add following to settings.py file to configure our cron jobs like its location and time:
CRONJOBS = [
    ('*/5 * * * *', 'myproject.cron.my_scheduled_job')
]

you can also define positional and keyword arguments which let you call django management commands:

CRONJOBS = [
    ('*/5 * * * *', 'myproject.myapp.cron.other_scheduled_job', ['arg1', 'arg2'], {'verbose': 0}),
    ('0   4 * * *', 'django.core.management.call_command', ['clearsessions']),
]

 

  • finally run this command to add all defined jobs from CRONJOBS to crontab (of the user which you are running this command with)
python manage.py crontab add

 

  • show current active jobs of this project
python manage.py crontab show

 

  • removing all defined jobs is straight forward
python manage.py crontab remove

 

NOTE: Run “python manage.py crontab add” each time you change CRONJOBS in any way!

9 thoughts on “How to setup Django cron jobs?

  1. My cronjob shows active when i enter command on terminal.
    1302c5b6006890e63b5ed3eaf7d5136a -> (‘*/2 * * * *’, ‘ethnicwholesaler.apps.importp.cron.update_product_quantity’)
    But nothing happend in my logic.

    Like

  2. Hey Hiten, i followed the same steps u have shown,

    adding cronjob: (a27d08d09ffb48830374b9c1d38f23f2) -> (‘* * * * *’, ‘products.cron.my_scheduled_job’)

    my scheduled job is also added but it not executing. i want it to get execute every minute.
    Can you help me with that?

    Like

  3. Hi, I am also quite new in python and programming.
    Trying to implement this, but somehow failing. when I run “manage.py crontab run processID” function get executed. But it is not being executed automatically.

    Is there a way to see (log) if task was executed? to specify some output file or something?

    Like

  4. Hey, I am trying to run python script with cron by using your approach on localhost with windows 7 but get an error ‘No module named fcntl’. please suggest me for sorting out this problem.

    Thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s