I am trying to run a cronjob in a debian jessie container. No matter what i try the job is not getting executed. For testing all I am trying to run is a simple echo “Hello”. I have googled this and it seems a lot of people have issues with this. I’ve seen many different problems and solutions. but none have worked for me. I have tried putting the cronjob both in /etc/cron.d and in root’s crontab, but no joy. Anyone have any ideas as to what I could be missing or doing wrong?
The container is verifiably running for this entire duration?
Is the cron
process running? Without it no process will pick up the crontab entry since it is unique to the container (host crontab cannot possible know to execute container’s entries since they are mounted in their own unique filesystem).
Yes, the container was up for over 24 hours. Also I put in jobs to test with the time a few minutes in the future. And I tried with both local time and UTC.
This should work just fine. I recommend putting together a POC that works and then verifying what’s the missing piece in your case.
Example.
Consider hello-cron
and cron.Dockerfile
:
hello-cron
:
* * * * * echo "Hello $(date)" >>/var/log/cron.log 2>&1
#
cron.Dockerfile
FROM debian
RUN apt-get update && apt-get install -y cron
COPY hello-cron /etc/cron.d/hello-cron
RUN crontab /etc/cron.d/hello-cron
CMD ["cron", "-f"]
build:
$ docker build -f cron.Dockerfile -t nathanleclaire/cron .
run:
$ docker run -d nathanleclaire/cron
<id>
checkup later:
$ docker exec -ti <id> cat /var/log/cron.log
Hello Wed Jul 6 20:52:01 UTC 2016
Hello Wed Jul 6 20:53:01 UTC 2016
Hello Wed Jul 6 20:54:02 UTC 2016
Hello Wed Jul 6 20:55:01 UTC 2016
$
If you still have issues, why not post a minimally reproducible example? We have no way of knowing what you might be doing wrong.
- N
I got this working from reading this SO post and following the fix given.
My way is the following
a cron service in docker-compose.yml
cron:
build:
context: ./path/to/dockerfile
dockerfile: Dockerfile-dev
volumes_from:
- applications
command:
- '* * * * * echo "Hello $(date)" >>/var/log/cron.log 2>&1'
Dockerfile
FROM debian:jessie
RUN apt-get update && apt-get install -y cron && \
rm -r /var/lib/apt/lists/*
COPY cron-entrypoint.sh /cron-entrypoint.sh
RUN chmod +x /cron-entrypoint.sh
ENTRYPOINT ["/cron-entrypoint.sh"]
#!/bin/bash
touch /var/spool/cron/crontabs/root
if [ -n "$1" ]; then
args=("$@")
argn=$#
for i in $(seq $argn)
do
echo "${args[$i-1]}" >> /var/spool/cron/crontabs/root
done
fi
cp /var/spool/cron/crontabs/root /tmp/temp.txt
printenv | cat - /tmp/temp.txt | tee /var/spool/cron/crontabs/root
chmod 600 /var/spool/cron/crontabs/root
cron -f
I tried but it does not work.I can see the crontab -l and also under top but still cron is not writing to log file.
You can provisionally do the following the see the cause,
$ apt-get install rsyslog
$ rsyslogd
$ service cron restart
$ tail -f /var/log/syslog
I had similar issue. In my case I was missing the username in the crontab line.
Jan 2 09:33:28 bdd4d02b73fb cron[802]: (CRON) STARTUP (fork ok)
Jan 2 09:33:28 bdd4d02b73fb cron[802]: Error: bad username; while reading /etc/cron.d/my-cron
Jan 2 09:33:28 bdd4d02b73fb cron[802]: (*system*my-cron) ERROR (Syntax error, this crontab file will be ignored)
Jan 2 09:33:28 bdd4d02b73fb cron[802]: (CRON) INFO (Skipping @reboot jobs -- not system startup)