Issue
I have a docker image part of a service and I am testing the possibility to add a cron job
I have setup the Dockerfile using with a crontab that should run a script (that for now should just output the date).
supervidord starts and spawns cron, but I see no regular outputs of dates...neither on terminal nor on the log file.
the Dockerfile
is:
FROM docker.io/python:3.6-alpine
ENV PYTHONUNBUFFERED 1
WORKDIR /opt/app-root/src
RUN apk update && apk add --no-cache bash supervisor \
&& rm -rf /var/cache/apk/*
# Copy Scripts
COPY mirror/src/ $WORKDIR
COPY mirror/src/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN chmod +x ./entrypoint.sh
RUN chmod +x ./run.sh
RUN touch logs.log
RUN /usr/bin/crontab ./crontab.txt
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
the supervisord.conf
file is:
[supervisord]
nodaemon = true
[program:cron]
command=crond -f
user=root
autostart=true
autorestart=true
crontab.txt
is:
*/10 * * * * * bash ./run.sh >logs.log 2>&1
and the run.sh
script is:
#!/bin/bash
echo `date +%Y%m%d_%H%M%S
The only output I see on terminal is:
crond[7]: USER root pid 8 cmd * bash ./run.sh >logs.log 2>&1
what's wrong with my setup?
Solution
found a way, posting my solution:
Dockerfile edited:
RUN chmod +x ./run.sh
# Setup CRON to update databases
RUN touch crontab.tmp \
&& echo '*/10 * * * * /opt/app-root/src/run.sh update > /dev/null' > crontab.tmp \
&& crontab crontab.tmp \
&& rm -rf crontab.tmp
# Start Server
EXPOSE 8080
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
and supervisord conf:
[supervisord]
nodaemon = true
logfile = /dev/null
logfile_maxbytes = 0
pidfile = /run/supervisord.pid
[program:mirror]
command = /bin/bash -c "/opt/app-root/src/run.sh"
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes=0
user = root
autostart = true
autorestart = true
startretries=10
priority = 20
[program:cron]
command = /bin/bash -c "/usr/sbin/crond -f -d 0"
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes=0
user = root
autostart = true
autorestart = true
priority = 20
In this example the container was exiting too quickly so added a startretries
option.
Answered By - bruvio Answer Checked By - Willingham (WPSolving Volunteer)