10👍
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 theCMD
in Dockerfile. - Added the command
python manage.py crontab add
to theCMD
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/```
3👍
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.
- [Django]-Django Channels 2.0: call Consumer's channel by its name
- [Django]-How to implement password change form in Django 1.9
- [Django]-How do I move project folders in PyCharm Project View?
- [Django]-Django logging error when using daemonize
1👍
i dont know this issue is fixed or not but i have the same problem and this docker file works for me:
FROM python:3.9-bullseye
RUN pip install --upgrade pip
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 -y
RUN apt-get install -y cron
RUN service cron start
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
WORKDIR /app
ENV PORT 8080
RUN python3 manage.py crontab add
RUN service cron start
CMD cron;gunicorn --bind :$PORT --workers 3 --timeout 300 usertest.wsgi:application
- [Django]-Django Rest Framework: PUT/POST fails in case of blank optional fields in nested serializer
- [Django]-Error when creating admin in django tutorial
- [Django]-Upgrading to django 1.11 many to many column not found
0👍
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.
0👍
-
Add django_crontab in INSTALLED_APPS in settings.py file
CRONJOBS = [
(‘5 * * * *’, ‘attendance.cron.print_message’, ‘>> /var/log/cron.log 2>&1’),
]
Here is my DockerFile
FROM python:3.9
ENV PYTHONUNBUFFERED=1
RUN apt-get update \
&& apt-get install gcc -y \
&& apt-get clean \
&& apt-get install gunicorn3 -y \
#&& apt-get install python3.9-dev -y \
&& apt-get install libcurl4-gnutls-dev librtmp-dev -y \
&& apt-get install libnss3 libnss3-dev iputils-ping -y \
&& apt-get install -y cron && touch /var/log/cron.log
RUN touch /var/log/attendance_system.log
WORKDIR /attendance_system
COPY requirements.txt /attendance_system/
RUN pip install -r requirements.txt
COPY . /attendance_system/
RUN chmod +x common_commands.sh
RUN bash common_commands.sh
EXPOSE 8000
EXPOSE 8001
Here is my common_commands.sh file
#!bin/bash
crontab -r # remove exist one
python manage.py crontab add # add
exec "$@"
Here is my web container here:
web:
container_name: attendance_system
build: .
command: bash -c "service cron start && daphne -b 0.0.0.0 -p 8000 AttendanceSystem.asgi:application"
# command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/attendance_system
ports:
- "8000:8000"
environment:
- TZ=Asia/Karachi
depends_on:
- db
- redis
Note:
- Please set files name as your project name i working with asgi
not wsgi - Make sure web container should run enter in that container and check python and container datetime
Commands:
sudo docker-compose exec -it **container_name or ID** /bin/bash
python
from datetime import datetime
print(datetime.now())
print will show your current datetime as your local machine if not then set Timezone of your docker-container as i set in web container.