Why is my django-crontab cronjob not executing?

Question:

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:

  • I am running this inside an Ubuntu Docker Conatiner on a VPS with nothing else on it.
Asked By: Merlin Duty

||

Answers:

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.

Answered By: Merlin Duty

Can’t comment cause i’m new

This exact same thing was happening to me until I manually ran it like you did (with the hash). Turns out I had a bug in my code so when it got to it, crontab automatically cancelled it since it was producing an error!

I’ve set up a 5 minute timer to validate that the job ran successfully and it did. I have the exact same setup as you do (+ some factories and api requests)

CRONJOBS = [('30 19 * * *', 'proctor.cron.proctor_job', '>> /home/atkisai/cron.log')]

Таким образом вы можете прочитать лог где крон напишет ошибку. Если проект находиться на рабочем столе у русских и он называется "Рабочий стол", то крон не будет работать. Также вы должны в systemctl проверить что ваш cron.service запущен и исправно работает.

Answered By: atkisai
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.