A Django app for running scheduled tasks (cron jobs) within your Django project. This is a fork of Tivix/django-cron with full Django 5 compatibility. Used by Ticketping in production.
- Simple API: Define cron jobs with a clean, Pythonic interface
- Django 5 Compatible: Fully tested and working with Django 5.x
- Flexible Scheduling: Support for various schedule types (daily, hourly, custom intervals)
- Multiple Locking Backends: File, cache, and database-based locking to prevent concurrent runs
- Job Logging: Track job execution history and debug issues easily
- No System Cron Required: Jobs run within your Django process
Install from PyPI:
pip install django-cron-django5Add django_cron to your INSTALLED_APPS:
INSTALLED_APPS = [
# ...
'django_cron',
]Run migrations:
python manage.py migrate django_cronCreate a cron job by subclassing CronJobBase:
from django_cron import CronJobBase, Schedule
class MyCronJob(CronJobBase):
RUN_EVERY_MINS = 60 # Run every hour
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'myapp.my_cron_job' # Unique code
def do(self):
# Your code here
passRegister your cron job in settings:
CRON_CLASSES = [
'myapp.cron.MyCronJob',
]Run cron jobs via crontab or similar. This is the most simplest and efficient way.
python manage.py runcronsIf you'd prefer to run via systemd or similar, use cronloop.
python manage.py cronloopHere's an example service file
[Unit]
Description=Django Cron Loop Service
After=network.target postgresql.service
[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/loc-backend
Environment="PATH=/home/ubuntu/.pyenv/versions/locenv/bin:/usr/local/bin:/usr/bin:/bin"
Environment="PYTHONUNBUFFERED=1"
# THE KEY CHANGES - Add these:
# Limit total iterations, then restart (prevents memory leaks)
# Runs ~50 times (50 * 2 min = 100 minutes) then restarts
ExecStart=/home/ubuntu/.pyenv/versions/locenv/bin/python manage.py cronloop --sleep 120
# Set memory limits (adjust based on your server)
MemoryMax=512M
MemoryHigh=400M
# Always restart when process exits
Restart=always
RestartSec=10
# Restart every 2 hours regardless (prevents long-term memory leaks)
RuntimeMaxSec=7200
# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=django-cronloop
# Security
NoNewPrivileges=true
PrivateTmp=yes
[Install]
WantedBy=multi-user.target- Python >= 3.9
- Django >= 4.2, < 6.0
For detailed documentation, please visit the GitHub repository.
We welcome contributions! Please see our GitHub repository for more information.
This project is licensed under the MIT License - see the LICENSE file for details.
- Original project: Tivix/django-cron
- Maintained for Django 5 by Ticketping
- Python >= 3.9
We suggest using pyenv to easily manage python versions.
- Install and activate python:
pyenv install 3.11.13
pyenv virtualenv 3.11.13 djcron
pyenv local djcron- Install project requirements:
pip install -r requirements-dev.txt- Install pre-commit hooks (if
pre-commitis installed globally):
pre-commit installpython testmanage.py test django_cron