django_crontab not working on production docker

Viewed 3329

I'm trying to run a scheduled script using django_crontab twice a day. I have tested it locally on ubuntu 18 and it works. However, when I'm trying to run it on the server (python Docker image that runs Django project on remote Ubuntu server) nothing happened. Here is my code:

setting.py:

INSTALLED_APPS = (
.
.
    'django_crontab',
.
.
.
)

RONJOBS = [
    ('0 9,21 * * *', 'appname.folder.file.start')
]

I executed cron by:

$ python manage.py crontab add .

and see the active job:

$ python manage.py crontab show
f95300a5599dc7687ac79ab51c8bb33c -> ('0 9,21 * * *', 'appname.folder.file.start')

def start():
  logger.info(" process begins!")
  do_something()

Note The function 'start' was tested in the production server and it works.

I also tried to map the chronjob logs by adding path to the CRONJOB at setting.py:

('0 9,21 * * *', 'appname.folder.file.start','>> /Logs/crontab.log')

but the file crontab.log wasn't created.

Any suggestions? Thanks!

3 Answers

I came across the same problem and this is what I did to fix the issues mentioned before:

  • Installed cron in the container.
  • Added the command service cron start to the CMD in Dockerfile.
  • Added the command python manage.py crontab add to the CMD in Dockerfile.

After spinning up the containers, I checked if the cronjobs were created with command python manage.py crontab show and crontab -l. In both situations I can see the cronjobs.

I tested it again and it's working. I used the following:

docker-compose
command: bash -c "service cron start && python manage.py crontab add && gunicorn serv.wsgi:application --bind 0.0.0.0:8000 --timeout 600 --workers 5" or the development server from Django. python manage.py runserver 0:8000

Dockerfile

FROM  python:3.9

RUN apt-get -y update

# Install cron.
RUN apt-get install -y cron && touch /var/log/cron.log

WORKDIR /app
COPY ./docker/python/django/requirements.txt /app/

RUN pip install -r requirements.txt

COPY ./app/ /app/```

Django-crontab depends on the cron daemon to be running, but many docker images do not come with it installed or start it otherwise. That's an init program's responsibility and most of them are stripped of that.

Have you checked the status of cron? I had the same situation on a production environment and realized that was the issue.

First check that the cronjob is properly installed by running crontab -l, this may depend on the user your app is running as, so maybe you'll have to add other options to that command.

If the crontab is installed, then check the status of the cron daemon by running service cron status. If it's not started, run service cron start and check if after that your cron tasks work. You should probably add this to your startup script.

If you are using Environment variable in production, it may happen that your cronjob can't access it. Following information may help,

If you are using Ubuntu >=18.0, try with following changes. I am assuming you have successfully integrated django-crontab with your django application. Now, access to your server via ssh and type "crontab -e", You will see your cronjobs are defined here. Add Your Environment Variables just above your cronjob. Please have a look at the following piece of lines to understand how it can make sense to your problem-

DB_NAME = "your database name"
DB_USER = "User"
DB_PASSWORD = "password"
SECRET_KEY = "************************"

*/1 * * * * /home/sms/sms/env/bin/python /home/sms/sms/sms- 
backend/website/manage.py crontab run 446aeeebac8c8f82d9e01bb662dc9b21 >> 
/tmp/scheduled_job.log # django>

Hope you get rid of it.

Related