backend/django

Django: use crontab easy in django - django-crontab

seul chan 2018. 4. 4. 11:32

Install

Insatll via pip

pip install django-crontab

And add it to INSTALLED_APPS

INSTALLED_APPS = (
    'django_crontab',
    ...
)

Make cron.py

Create cron.py in project directory (same directory as settings.py)

def my_scedulaer():
    # do sth
    pass

And add CRONJOBS in settings.py

CRONJOBS = [
    ('*/5 * * * *', 'myapp.cron.my_scheduled_job'),
    # also can add args, kwargs
    ('*/5 * * * *', 'myapp.cron.other_scheduled_job', ['arg1', 'arg2'], {'verbose': 0}),
]

Commands

# showing current crontab jobs
python manage.py crontab show

# adding crontab
python manage.py crontab add

# remove all jobs
python manage.py crontab remove

If you want more information, please check django-crontab github(here)