Tri Nguyen
Tri Nguyen

Reputation: 11108

cron job not running inside docker container on ubuntu

I have a simple Dockerfile as follows

FROM ubuntu:latest

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

RUN chmod a+x /etc/cron.d/test-cron
RUN touch /var/log/cron.log

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

and the content of crontab file is as simple as

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# empty line

When I run this on my local OS X machine (with docker-machine running), it works fine ("Hello world" is printed to log file every minute). However, when I try to run it on an Ubuntu machine, the cron job does not run (empty log file).

Here's the command I use to run the container

docker build -t crontest .
docker run --name cron crontest

I am not sure why this would be the case. I wonder if something is wrong with the Ubuntu box that I have (wrong time setting?). I have tried to restart that machine to no effect. I currently do have other docker containers running on the Ubuntu box and they're running fine.

Any suggestion on what I could do to debug/ fix this would be hugely appreciated.

EDIT:

After going inside the container (docker exec -it cron /bin/bash), I can verify that cron is running there:

root@a2ad451af8d9:/# ps -ef | grep cron
root         1     0  0 20:15 ?        00:00:00 /bin/sh -c cron && tail -f /var/log/cron.log
root         6     1  0 20:15 ?        00:00:00 cron
root         7     1  0 20:15 ?        00:00:00 tail -f /var/log/cron.log
root        25    11  0 20:21 ?        00:00:00 grep --color=auto cron

Upvotes: 32

Views: 25679

Answers (5)

Vipin Bihari Tiwari
Vipin Bihari Tiwari

Reputation: 35

It has been fixed in debian recently: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=726661 and in Ubuntu Wily (15.10).

As a workaround you can try commenting the module pam_loginuid.so inside /etc/pam.d/cron and restart cron (or the docker container).

Upvotes: 0

Archimedes Trajano
Archimedes Trajano

Reputation: 41310

In my case when I COPY a file it created a secondary hard link (it may be a Windows thing).

What I had to do was make sure the file was created inside a running container.

FROM mysql:5.7
RUN apt-get update && apt-get install -y anacron
COPY crontab /tmp/crontab

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Run the command on container startup
CMD (cat /tmp/crontab > /etc/cron.d/hello-cron ) && cron && tail -f /var/log/cron.log

Upvotes: 0

GameScripting
GameScripting

Reputation: 17012

I had a backup script called backup.sh that I copied to /etc/cron.daily. The script was not called properly.

To make it to I had to rename to the to just backup without the .sh

So for me ls -l /etc/cron.daily had the following output:

root@0989a35b8f94:/# ls -l /etc/cron.daily
total 24
-rwxr-xr-x 1 root root 1474 Sep 13 16:47 apt-compat
-rwxrwxr-x 1 root root   45 Nov  9 11:18 dobackup
-rwxr-xr-x 1 root root 1597 Feb 22  2017 dpkg
-rwxr-xr-x 1 root root 4125 Mar  2  2016 exim4-base
-rwxr-xr-x 1 root root  249 May 17 11:59 passwd

To test/analyse this I used the following approach:

I looked up the crontab file cat /etc/crontab showing the following line for the daily cronjobs:

25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

I then isolated my backup script into a dedicated folder:

mkdir /etc/cron.test
mv /etc/cron.daily/dobackup /etc/cron.test

Then by running

test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.test)

and using ps auxf on a different terminal showed me that the jobs now is running. You can also verify that it breaks when renaming to the .sh version:

mv /etc/cron.test/dobackup /etc/cron.test/dobackup.sh
test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.test)

It then exists immediately, so no job is run.

Upvotes: 0

Ameya
Ameya

Reputation: 83

I had similar issue, specifically with Ubuntu 14.04. To debug, I tried running cron in foreground, and found it to emit System error messages while trying to run the scheduled jobs.

Apparently, its a known issue with --net=host parameter (ref: https://github.com/moby/moby/issues/5899). I tried passing --pid=host as suggested, and with that, the cron jobs started running fine.

Upvotes: 3

Dzamo Norton
Dzamo Norton

Reputation: 1389

Install rsyslog inside the container with apt-get install rsyslog and launch it with the command rsyslogd before starting cron with cron -L15 (maximum logging). Then watch the file /var/log/syslog inside the container to see the cron daemon's own log output. It will tell you if there was a problem parsing your crontab and it will, in your case, log an entry every minute similar to the below if it has registered and is trying to run your job.

CRON[16]: (root) CMD (echo "Hello world" >> /var/log/cron.log 2>&1)

Upvotes: 41

Related Questions