I am using Django-Crontabs to execute my function every day in the morning at 6 am. Here is my code for Django-Crontabs.
Firstly I install Django-Crontabs with this command. pip3 install Django-crontab
Settings.py
INSTALLED_APPS = [
'django_crontab',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp'
]
CRONJOBS = [
('* 6 * * *', 'myapp.cron.cronfunction')
]
Here I have set the code to run every day at 6 AM. As per the following template. But it is not working.
# Use the hash sign to prefix a comment
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
cron.py
def cronfunction():
logger.warning("========== Run Starts ====")
logger.warning("========== Run Ends ======")
If I am running this function every 2 or 3 minutes then it works fine.
('*/2 * * * *', 'myapp.cron.cronfunction')
How do I set my cronjob to run every day, Can anyone help? please.
Change your settings.py to:
# [...]
CRONJOBS = [
('0 6 * * *', 'myapp.cron.cronfunction')
]
0 specifies always at minute zero. 6 specifies always at hour six. Star means "every"; here day and month.
I can really recommend this visualization tool for cronjobs.