See cron output via docker logs, without using an extra file

Alpine: No need for redirection

using the default cron utility (busybox)

Dockerfile

FROM alpine:3.7

# Setting up crontab
COPY crontab /tmp/crontab
RUN cat /tmp/crontab > /etc/crontabs/root


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

crontab

* * * * * echo "Crontab is working - watchdog 1"

Centos:

Redirection to /proc/1/fd/1 inside the crontab declaration line

Dockerfile

FROM centos:7

RUN yum -y install crontabs

ADD crontab /etc/cron.d/crontab
RUN chmod 0644 /etc/cron.d/crontab
RUN crontab /etc/cron.d/crontab


CMD ["crond", "-n"]

crontab

* * * * * echo "Crontab is working - watchdog 1" > /proc/1/fd/1

enter image description here


@mcfedr is correct, but it took me a while to understand it with it being a one-liner with variables and some extra code related to setting up cron.

This may be a little bit easier to read. It helped me to write it out explicitly.

# Create custom stdout and stderr named pipes
mkfifo /tmp/stdout /tmp/stderr
chmod 0666 /tmp/stdout /tmp/stderr

# Have the main Docker process tail the files to produce stdout and stderr 
# for the main process that Docker will actually show in docker logs.
tail -f /tmp/stdout &
tail -f /tmp/stderr >&2 &

# Run cron
cron -f

Then, write to those pipes in your cron:

* * * * * /run.sh > /tmp/stdout 2> /tmp/stderr

Change your cron file to below

07 2 * * * /data/docker/backup_webserver/backupscript.sh > /dev/stdout

This will make sure the logs go to the container output

Tags:

Docker

Logging