Problem running Django crontab in Docker container
I'm trying to create a simple cron, in my django application, but despite adding and launching correctly, the cron doesn't execute, nor does it save logs
settings.py
INSTALLED_APPS = [
'django_crontab',
]
CRONJOBS = [
('*/1 * * * *', 'myapp.cron.test_func', '>> /var/log/cron.log 2>&1'),
]
Dockerfile
python:3.12
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt .
RUN pip3 install --upgrade pip
RUN pip3 install --no-cache-dir -r requirements.txt
RUN apt-get update && \
apt-get install -y cron && \
apt-get clean
RUN touch /var/log/cron.log
docker-entrypoint.sh
python3 manage.py crontab add
service cron start
exec "$@"
When the container is launched, the commands return these results:
crontab -l
*/1 * * * * /usr/local/bin/python3 /app/manage.py crontab run <cron_id> >> /var/log/cron.log 2>&1 # django-cronjobs for my_project
python3 manage.py crontab show
<cron_id> -> ('*/1 * * * *', 'myapp.cron.test_func', '>> /var/log/cron.log 2>&1')
service cron status
cron is running.
Manually running cron with the command “python3 manage.py crontab run <cron_id>” works fine, the logs are empty.
the django-crontab project last update was in 2016.. over 8 years ago. It's probably a dead project.
I'd recommend to implement a custom django admin command and register it with crontab the old fashioned way (crontab -e).
in a bash file, setup your virtual environment then call the custom admin script similar to below python manage.py <script_name> --settings=<django_app>.settings
schedule the script to run in cron (e.g.): 30 * * * * <path_to_script>/<script_name>.sh
if adding your bash file to git, make sure you set your .sh file to maintain execute. for example, C:\Temp\TestRepo>git update-index --chmod=+x foo.sh