pythondjangodjango-crontab

Why is my django-crontab cronjob not executing?


I have a django-project with an app called app that has a file called cron.py with a function called main_routine().

I want the main_routine() function to be called every minute.

In my django-project/django-project/settings.py I have this:

INSTALLED_APPS = [
    'django_crontab',
    ...
]

...

CRONJOBS = [
    ('*/1 * * * *', 'app.cron.main_routine')
]

My django-project/app/cron.py looks like this:

from app.models import SomeModel
from django.utils import timezone

def main_routine():
    object = SomeModel.objects.get(name='TestObject1')
    object.updated = timezone.now()
    object.save()

Of course I ran : python3 manage.py crontab add And the terminal printed:

adding cronjob: (someHash) -> ('*/1 * * * *', 'app.cron.main_routine')

To be safe I run: python3 manage.py crontab show And the terminal prints:

Currently active jobs in crontab:
someHash -> ('*/1 * * * *', 'app.cron.main_routine')

To check if evrything works I run: python3 manage.py crontab run someHash

Then I take a look at the admin page and see that TestObject1 has an updated datetime of just now. (so far everything seems to be gooing smoothly)

The main issue: No matter how long I wait the job will not be executed automatically.

What am I doing wrong?

some Background info:


Solution

  • First: I still do not know why crontab is not working.

    However I found a way around this issue.

    You can use the python advanced scheduler aka. apscheduler, to substitute crontab.

    The idea is to write a module that has your desired functionality in it, and wire it into on of your apps AppConfig in its apps.py file.

    There is a great walkthrough in this article.