Skip to content

Instantly share code, notes, and snippets.

@vivekrk1992
Last active October 26, 2020 11:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vivekrk1992/92f13b5f85a675cd563821f3c4cf0a61 to your computer and use it in GitHub Desktop.
Save vivekrk1992/92f13b5f85a675cd563821f3c4cf0a61 to your computer and use it in GitHub Desktop.
reference url
"""
https://pypi.org/project/django-crontab/
"""
install cron tab for django
"""
pip install django-crontab
"""
add it to installed apps in django settings.py:
"""
INSTALLED_APPS = (
'django_crontab',
...
)
"""
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()
"""
now add this to your settings.py:
"""
CRONJOBS = [
('*/5 * * * *', 'app_name.cron.hi')
]
"""
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']),
]
"""
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
enjoy -)
@vivekrk1992
Copy link
Author


1 2 3 4 5

  1. Minute
  2. Hour
  3. day of the month (1, 31)
  4. Month
  5. Day of the week (0 to 7) 0 or 7 is Sunday, 1=mon, Tues = 2 and sat=6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment