Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active February 29, 2024 11:18
Show Gist options
  • Save x-yuri/f17b2cefc6e673e979850d79b265f345 to your computer and use it in GitHub Desktop.
Save x-yuri/f17b2cefc6e673e979850d79b265f345 to your computer and use it in GitHub Desktop.
docker: cron && tail -f

docker: cron && tail -f

If cron dies, the container keeps running.

docker-compose.yml:

services:
  cron:
    build: .

Dockerfile:

FROM ubuntu:23.04
COPY crontab .
RUN set -x \
    && apt-get update \
    && apt-get install cron \
    && crontab crontab \
    && touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log

crontab:

* * * * * date >>/var/log/cron.log 2>&1
$ docker-compose up

$ docker-compose ps
NAME                      COMMAND                  SERVICE             STATUS              PORTS
cron-experiments-cron-1   "/bin/sh -c 'cron &&…"   cron                running             

$ docker-compose exec cron ps -efH
UID          PID    PPID  C STIME TTY          TIME CMD
root          10       0  0 13:34 ?        00:00:00 ps -efH
root           1       0  0 13:34 ?        00:00:00 /bin/sh -c cron && tail -f /var/log/cron.log
root           8       1  0 13:34 ?        00:00:00   cron
root           9       1  0 13:34 ?        00:00:00   tail -f /var/log/cron.log

$ docker-compose exec cron pkill cron

$ docker-compose exec cron ps -efH
UID          PID    PPID  C STIME TTY          TIME CMD
root          28       0  0 13:36 ?        00:00:00 ps -efH
root           1       0  0 13:34 ?        00:00:00 /bin/sh -c cron && tail -f /var/log/cron.log
root           9       1  0 13:34 ?        00:00:00   tail -f /var/log/cron.log

$ docker-compose ps
NAME                      COMMAND                  SERVICE             STATUS              PORTS
cron-experiments-cron-1   "/bin/sh -c 'cron &&…"   cron                running             
@dlidstrom
Copy link

@x-yuri Thank you 🙏

@derhecht
Copy link

What happens when you also kill the "tail" process?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment