Django Crontab
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
-
No jobs running / Job not executing automatically
cause This is often caused by the underlying cron daemon not running on the system/container, incorrect paths, or not having run `python manage.py crontab add` after configuration.fix1. Ensure you've run `python manage.py crontab add`. 2. Verify the `cron` service is active on your system (`sudo service cron status` or check Docker logs). 3. Check logs for the cron job's output (if configured) for specific errors. 4. Run `python manage.py crontab show` to confirm jobs are listed. -
Unknown command: 'crontab' or 'manage.py crontab' command not found
cause The `django_crontab` app has not been added to your `INSTALLED_APPS` in `settings.py`, or the package is not installed in the environment.fixEnsure `django_crontab` is included in your `INSTALLED_APPS` list and that `pip install django-crontab` was executed successfully in the environment where `manage.py` is run. -
Jobs appear 'encrypted' or hashed in `crontab -e` output, making manual editing difficult.
cause This is intended behavior of `django-crontab`. It uses hashes to uniquely identify and manage jobs defined in `settings.CRONJOBS`, preventing conflicts and ensuring consistency.fixDo not manually edit `crontab -e` entries generated by `django-crontab`. All job modifications (add, change, remove) should be done by adjusting the `CRONJOBS` setting and then running the appropriate `python manage.py crontab` command.
Warnings
- breaking The project is effectively abandoned, with the last release in March 2016. This means critical bugs, security vulnerabilities, or incompatibilities with newer Python/Django versions (beyond Django 2.0) are unlikely to be addressed.
- gotcha You MUST run `python manage.py crontab add` every time you modify the `CRONJOBS` setting in your `settings.py`. Forgetting this step is a very common reason why scheduled tasks do not run as expected.
- gotcha The library does not work on Windows operating systems, as it relies on the underlying Unix `crontab` utility.
- gotcha When running in Docker or other containerized environments, the cron daemon might not be running by default, or the Python executable path might be incorrect. This can lead to jobs being 'added' but never executing.
Install
-
pip install django-crontab
Imports
- CRONJOBS (setting)
CRONJOBS = [('*/5 * * * *', 'myapp.cron.my_scheduled_job')]
Quickstart
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