Germano
Germano

Reputation: 636

django-crontab not working on docker python:3.9-buster image

i am trying to run a cronjob every minute, as a test, on my django app with django_crontab. I tryed running 2 cronjobs, one with django_crontab other with my cron

PART 1 django_crontabs

1- I already installed django_crontab and added in my requirements.txt

   pip install django-crontab
   django-crontab==0.7.1

2- Added to my settings.py

    INSTALLED_APPS = [
        'django_crontab',
         ...
    ]

    CRONJOBS = [
        ('*/1 * * * *', 'myapp.cron.my_scheduled_job', '>> /var/log/cron.log 2>&1'),
    ]

3- Created a file cron.py inside folder myapp

from datetime import datetime


def my_scheduled_job():
    date = datetime.today().date()
    print(date)
    print('RUNNING CRONTAB YESSSSSSSSSSS!!!!!!!')
    pass
    return True

4- My Dockerfile

FROM python:3.9-buster

WORKDIR /app

COPY ./requirements.txt .

RUN apt-get update && apt-get install libpq-dev gcc python3-dev musl-dev libffi-dev -y && apt-get install -y cron
RUN pip install uwsgi
RUN pip install -r requirements.txt

RUN export PYTHONPATH=$PYTHONPATH:/app/
COPY src/hello-cron /etc/cron.d/hello-cron
RUN chmod 0644 /etc/cron.d/hello-cron
RUN crontab /etc/cron.d/hello-cron
RUN touch /var/log/cron.log

COPY ./src .
ENV PORT=8000
EXPOSE 8000
# Runner script here
CMD ["sh", "/app/runner.sh"]

5- My docker-compose file

version: '3.3'

services:
    app:
        build: .
        command: bash -c "python3 ./manage.py crontab add && python3 ./manage.py runserver 0.0.0.0:8000"
        ports:
            - 8000:8000
        volumes:
            - ./src:/app/:Z

PART 2 cronjobs linux

6- I created a file called hello-cron , and used above on the Dockerfile, with current infos:

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1

When I run sudo docker-compose up --build, I get on my docker logs

app_1       |   adding cronjob: (0c7b4daeba89b95a59287e280193e9ca) -> ('*/1 * * * *', 'myapp.cron.my_scheduled_job', '>> /var/log/cron.log 2>&1')
app_1       | Watching for file changes with StatReloader

7- If i access my container, via, sudo docker exec -it app_1 bash and run crontab -l , i get:

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
*/1 * * * * /usr/local/bin/python3 /app/manage.py crontab run 0c7b4daeba89b95a59287e280193e9ca >> /var/log/cron.log 2>&1 # django-cronjobs for portal

But when I check my /var/log/cron.log it is empty...

What should i do?

Upvotes: 0

Views: 1198

Answers (1)

Iamara Alvarez
Iamara Alvarez

Reputation: 1

I was having a similar problem with crontab in Django 4 and docker. The service was up (you can check with service cron status), crontab was installed but was not executing.

My steps was to restart the service and then I found an error on log. When crontab executes try to build settings.py and couldn't find any env even though they were there.

My problem was that when crontab was running in a docker container and for some reason, was not able to read the container's envs but when I ran manually worked.

My workaround for that was to copy docker envs to .env file project root and then my cron worked perfectly.

env > app/.env

Upvotes: 0

Related Questions