Docker containers interfere with cron jobs

Permissions, domain/LDAP, power, security, notification and more.
Locked
User avatar
djonesuk
Posts: 23
Joined: 28 Jun 2021, 22:01

Docker containers interfere with cron jobs

Post by djonesuk »

Hi

I noticed that cronjobs were not running whilst I had active docker containers. After a bit of troubleshooting I found this was down to cron daemons running inside containers confusing the TOS cron init script. This is because the pidof command will return the pid of any containerized cron daemons rather than just the main TOS daemon's pid.

To fix this I editted /etc/init.d/cron as follows:

Replace all instances of

Code: Select all

pid=`pidof crond`
with

Code: Select all

local pid=`ps -e -o pid,cgroup,args | awk '$2=="-" && /crond/ && !/awk/ { print $1}'`
This fixed this issue.
User avatar
brown7
Posts: 1
Joined: 06 Nov 2021, 01:44

Re: Docker containers interfere with cron jobs

Post by brown7 »

Let’s create a new file called "hello-cron" to describe our job.

* * * echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.
If you are wondering what is 2>&1, Ayman Hourieh explains.

The following Dockerfile describes all the steps to build your image

FROM ubuntu: latest
MAINTAINER docker@ekito.fr

RUN apt-get update && apt-get -y install cron

# Copy hello-cron file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /etc/cron.d/hello-cron

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

# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
(see Gaafar's comment and How do I make apt-get install less noisy?:
apt-get -y install -qq --force-yes cron can work too)
Locked