Search code examples
pythondockercron

Crontab doesn't run the python script inside a docker


Dockerfile:

FROM python:3.9


WORKDIR /mydir

# Set desired Python version
ENV python_version=3.8 \
        PYTHONPATH="/mydir/smart-git/smart-datascience/python_daemons/Virtual/app:/mydir/smart-git/smart-datascience/data/load/:/mydir/smart-git/smart-datascience/Andrew/Monitoring kalibratora/":$PYTHONPATH
RUN env

ENV TZ=Europe/Berlin
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezoneA

COPY requirements.txt requirements.txt
USER root
RUN pip3 install --user -r requirements.txt

RUN apt-get update && apt-get install -y cron

ADD mydir-crontab /etc/cron.d/mydir-crontab

RUN chmod 0644 /etc/cron.d/mydir-crontab

RUN crontab /etc/cron.d/mydir-crontab


CMD cron && python3 /mydir/smart-git/smart-datascience/python_daemons/Virtual/app/app.py

Crontab file:

20 * * * * /usr/local/bin/python3 /mydir/smart-git/smart-datascience/python_daemons/Virtual/app/jsonification.py

The app.py script works without any issues. However, the jsonification.py doesn't. I've tested jsonification.py for over 10 days of continuous runtime and it worked without issues so I'm assuming I messed up something with docker/crontab.

EDIT: For additional context, jsonification.py runs some processing on data and then stores it to a json file (creates a new one if it doesn't exist and appends if it does).


Solution

  • You're invoking 2 Python scripts: one inside the cronjob and one with the last CMD command of the Dockerfile. Leave it only inside the cronjob.

    So, replace the last line of the Dockerfile with

    CMD cron && tail -f /var/log/cron.log
    

    in order to execute the cron-job on container startup and see its logs.