Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

logging

I am running "cron" in a docker container.
Every day a script is executed.
The output of this script I would like to see via "docker logs "

The process with PID 0 is the cron daemon in my container. Entrypoint starts cron in foreground:

/usr/sbin/crond -f 

I understand, that I could redirect the script output to a file "path/to/logs"

07 2 * * * /data/docker/backup_webserver/backupscript.sh >> path/to/logs

and start the container as following to see the logs

"tail -f path/to/logs" 

But then the file "path/to/logs" would grow during the runtime of the container.
Is there a possibility to log from crontab, directly to "docker logs" ?

like image 585
Skip Avatar asked Jul 30 '17 00:07

Skip


2 Answers

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

like image 178
Tarun Lalwani Avatar answered Oct 14 '22 07:10

Tarun Lalwani


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

like image 45
MaxBlax360 Avatar answered Oct 14 '22 07:10

MaxBlax360