Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active February 29, 2024 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

It should restart, no?

@x-yuri
Copy link
Author

x-yuri commented Jan 13, 2024

@dlidstrom Why would it? Programs don't restart just because we want them to.

@dlidstrom
Copy link

Not sure what you’re demonstrating (a problem or a solution?). If cron dies then the pod is no longer working. Is this what you’re demonstrating? I’m interested in a robust cron solution with Docker.

@x-yuri
Copy link
Author

x-yuri commented Jan 13, 2024

@dlidstrom I'm demonstrating a problem. A solution? E.g. this Stack Overflow answer. You can also check out other answers and gists I made around that time.

@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