Django Crontab

0.7.1 · abandoned · verified Thu Apr 16

django-crontab is a Python library providing dead-simple job scheduling for Django applications, leveraging the system's `crontab` utility. The latest version is 0.7.1, released in March 2016. Due to its age and lack of recent updates, it is best suited for legacy Django projects (up to Django 2.0) or users prepared to address potential issues themselves.

Common errors

Warnings

Install

Imports

Quickstart

To quickly set up `django-crontab`, first add `django_crontab` to your `INSTALLED_APPS`. Then, define your scheduled functions within a Python file (e.g., `myapp/cron.py`). Finally, specify these jobs in your `settings.py` using the `CRONJOBS` list, and activate them by running `python manage.py crontab add`. Remember to run `crontab add` after any changes to `CRONJOBS`.

import os

# settings.py
INSTALLED_APPS = [
    # ...
    'django_crontab',
    'myapp', # Your app containing the cron job
]

CRONJOBS = [
    # Run 'myapp.cron.my_scheduled_job' every 5 minutes
    ('*/5 * * * *', 'myapp.cron.my_scheduled_job'),
    # Example calling a Django management command daily at midnight
    ('0 0 * * *', 'django.core.management.call_command', ['clearsessions']),
    # Example with arguments and redirecting output to a log file
    ('0 0 * * 0', 'django.core.management.call_command', ['dumpdata', 'auth'], {'indent': 4}, f'> {os.environ.get('DJANGO_CRON_LOG_DIR', '/tmp')}/last_sunday_auth_backup.json'),
]

# myapp/cron.py
# Create this file in your Django app

def my_scheduled_job():
    # Your periodic task logic goes here
    print('My scheduled job ran!')
    # Example: Log to a file or database
    with open(f'{os.environ.get('DJANGO_CRON_LOG_DIR', '/tmp')}/django_crontab_log.txt', 'a') as f:
        f.write('Job executed at ' + str(os.time.time()) + '\n')

# After configuring, run in your terminal:
# python manage.py crontab add
# To view jobs:
# python manage.py crontab show
# To remove jobs:
# python manage.py crontab remove

view raw JSON →