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

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


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


Alpine: No need for redirection

using the default cron utility (busybox)

Dockerfile

FROM alpine:3.7# Setting up crontabCOPY crontab /tmp/crontabRUN cat /tmp/crontab > /etc/crontabs/rootCMD ["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:7RUN yum -y install crontabsADD crontab /etc/cron.d/crontabRUN chmod 0644 /etc/cron.d/crontabRUN crontab /etc/cron.d/crontabCMD ["crond", "-n"]

crontab

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

enter image description here


fifo is the way to go, it also useful because it allows cron tasks that are not running as root to write to the output.

I am using a CMD along these lines

ENV LOG_STREAM="/tmp/stdout"CMD ["bash", "-o", "pipefail", "-c", "mkfifo $$LOG_STREAM && chmod 777 $$LOG_STREAM && echo -e \"$$(env | sed 's/=\\(.*\\)/=\"\\1\"/')\n$$(cat /etc/cron.d/tasks)\" > /etc/cron.d/tasks && cron -f | tail -f $$LOG_STREAM"]

With the tasks in /etc/cron.d/tasks

* * * * */10 www-data echo hello >$LOG_STREAM 2>$LOG_STREAM

I also prepend the env at launch to tasks so it's visible to the tasks, as cron doesnt pass it though by itself. The sed is needed because crontab format requires env vars to be quoted - at least it requires empty vars to be quoted and fails to run tasks if you have an empty var without quotes.