Docker Community Forums

Share and learn in the Docker community.

Running cronjob in debian jessie container

docker

(Larrymartell59) #1

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?


(Nathan Le Claire) #2

The container is verifiably running for this entire duration?


(Nathan Le Claire) #3

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).


(Larrymartell59) #4

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.


(Nathan Le Claire) #5

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

(Larrymartell59) #6

I got this working from reading this SO post and following the fix given.


#7

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"]

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

(Vsaggu) #8

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.