0

I'm trying to launch a simple chron message inside a docker container to test chron :

Inside my php-fpm Dockerfile :

...

RUN echo "*       *       *       *       *       run-parts /etc/periodic/1min" >> /etc/crontabs/root

RUN mkdir /etc/periodic/1min

COPY cronscript.sh /etc/periodic/1min

RUN chmod a+x /etc/periodic/1min/cronscript.sh

RUN dos2unix /etc/periodic/1min/cronscript.sh

...

CMD [ "crond", "-l", "2", "-f" ]; composer install ; wait-for-it database:3306 -- bin/console doctrine:migrations:migrate ;  php-fpm;

My cronscript.sh :

#!/usr/bin/env sh
echo "My test message"

When i run crontab -e inside the container i got :

enter image description here

When i run the container i didn't get the message :

enter image description here

How can i correctly show the message periodically each 1 minute ?

Khaled Boussoffara
  • 1,567
  • 2
  • 25
  • 53

2 Answers2

0

Are you sure that cron jobs are added to the schedule?

  1. Try to define your jobs outside of the Docker image like cron.txt
    * * * * * xxx xxx
    
  2. Add the schedule to the image using ADD command in Dockerfile
    ADD cron.txt /etc/crontabs/xxx
    
lazylead
  • 1,453
  • 1
  • 14
  • 26
  • hello, inside my docker file i use this command : RUN echo "* * * * * run-parts /etc/periodic/1min" >> /etc/crontabs/root, and the i copy the script inside the 1min folder, should i copy the cript outside the docker image? – Khaled Boussoffara Feb 07 '21 at 08:36
  • Yes. In case if you want to add a custom file to the docker image you should use ADD or COPY docker command (here is the difference between them https://stackoverflow.com/a/24958548/6916890). – lazylead Feb 07 '21 at 16:59
  • In my case it works, the app in production for more than 4 years. Please ensure that jobs are placed in the proper location using ADD command, and cron itself started in ENTRYPOINT/CMD. – lazylead Feb 07 '21 at 20:16
  • how can i verify that cron itself started in ENTRYPOINT/CMD please – Khaled Boussoffara Feb 07 '21 at 21:05
0

Dockerfile

FROM alpine

# Copy script which should be run
COPY ./yourscript /usr/local/bin/yourscript
# Run the cron every minute
RUN echo '*  *  *  *  *    /usr/local/bin/yourscript' > /etc/crontabs/root

CMD ['crond', '-l 2', '-f']

Script

#!/bin/sh
echo "My test message" >> /dev/stdout
Ashok
  • 3,190
  • 15
  • 31
  • hello, this line was added to my crontab, when i run crontab -e i got this line : * * * * * /usr/local/bin/script , but i didn't get any message, look like the cron doesn't work – Khaled Boussoffara Feb 07 '21 at 08:32