1. Install django cron tab
pip install django-crontab
2. Add it to installed apps in django settings.py:
INSTALLED_APPS = (
'django_crontab',
...
)
3. Create one script with any file app_name.cron.py
def hi():
print('iam an cron job')
f = open('/home/host_name/django_cron.txt', 'w')
f.close()
4. Now add this to your settings.py:
CRONJOBS = [
('*/5 * * * *', 'app_name.cron.hi')
]
5. We can pass the argument also
CRONJOBS = [
('*/5 * * * *', 'myapp.cron.other_scheduled_job', ['arg1', 'arg2'], {'verbose': 0}),
('0 4 * * *', 'django.core.management.call_command', ['clearsessions']),
]
6. Run Command - we created cronjabs script and then add when to execute the script in setting.py also
Now we add the cronjob to initiate to run this command
python manage.py crontab add
We want to see initiated cronjobs execute this command
python manage.py crontab show
We want to remove initiated cronjob excute this command
python manage.py crontab remove
Now we check the home folder the 'django_cron.txt' file exist.
Reference