1

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.

Tarquinius
  • 1,468
  • 1
  • 3
  • 18
HMS
  • 97
  • 8
  • * 6 * * * means running cron every minute at 6 not once at 6:00 – PTomasz Apr 12 '23 at 06:31
  • So, how can I set cron at everyday 6 AM? – HMS Apr 12 '23 at 06:47
  • 2
    `'0 6 * * *'` This would be the way to go. Check [this visualization tool](https://crontab.guru/#0_6_*_*_*) out! 0 specifies always at minute zero. 6 specifies always at hour six. Star means every day and month. – Tarquinius Apr 12 '23 at 07:02
  • @Tarquinius, I will try this solution and tomorrow let you know it is working or not. Thanks in advance. – HMS Apr 12 '23 at 07:14
  • @Tarquinius, It is working properly, I got my result as expected. Thank you. – HMS Apr 13 '23 at 07:17

1 Answers1

0

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.

Tarquinius
  • 1,468
  • 1
  • 3
  • 18