通过docker日志查看cron输出,无需使用额外文件
我正在docker容器中运行“cron”通过docker日志查看cron输出,无需使用额外文件,docker,logging,Docker,Logging,我正在docker容器中运行“cron” 每天都会执行一个脚本 我希望通过“docker日志”查看此脚本的输出 PID为0的进程是容器中的cron守护进程。入口点在前台启动cron: /usr/sbin/crond -f 我知道,我可以将脚本输出重定向到文件“path/to/logs” 并按如下所示启动容器以查看日志 "tail -f path/to/logs" 但是,文件“path/to/logs”将在容器运行时增长 是否可以从crontab直接登录到“docker日志”将您的cron
每天都会执行一个脚本
我希望通过“docker日志”查看此脚本的输出
PID为0的进程是容器中的cron守护进程。入口点在前台启动cron:
/usr/sbin/crond -f
我知道,我可以将脚本输出重定向到文件“path/to/logs”
并按如下所示启动容器以查看日志
"tail -f path/to/logs"
但是,文件“path/to/logs”将在容器运行时增长是否可以从crontab直接登录到“docker日志”将您的cron文件更改为下面的
07 2 * * * /data/docker/backup_webserver/backupscript.sh > /dev/stdout
这将确保日志进入容器输出您可以使用FIFO
mkfifo path/to/logs
当进程通过FIFO交换数据时,内核传递所有
数据,而不将其写入文件系统。因此,FIFO特别版
文件系统上没有内容;文件系统条目仅为
参考点,以便进程可以使用中的
文件系统
fifo是一种方法,它也很有用,因为它允许不以root身份运行的cron任务写入输出 我在这些方面使用CMD
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"]
使用/etc/cron.d/tasks中的任务
* * * * */10 www-data echo hello >$LOG_STREAM 2>$LOG_STREAM
我还在启动时将env预先添加到tasks
,这样它对任务是可见的,因为cron本身不会传递它。之所以需要sed,是因为crontab格式需要引用env vars-至少它需要引用空vars,如果没有引号的var为空,则无法运行任务。Alpine:无需重定向
使用默认的cron实用程序(busybox)
Dockerfile
克朗塔布
Centos:
重定向到crontab声明行内的/proc/1/fd/1
Dockerfile
克朗塔布
@mcfedr是正确的,但我花了一段时间才理解它,因为它是一个带有变量的单行程序,以及一些与设置cron相关的额外代码
这可能更容易阅读。它帮助我把它明确地写出来
# 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
然后,写入cron中的管道:
* * * * * /run.sh > /tmp/stdout 2> /tmp/stderr
当开始使用crond-l2-f
?我认为/dev/stdout
实际上会转到/proc/self/fd/1
,对于cron启动的进程,这将不是docker监视的/proc/1/fd/1
文件。我认为这不会起作用,因为/dev/stdout是访问它的进程的stdout的链接。所以,通过执行foo>/dev/stdout,您是在说“将我的stdout重定向到我的stdout”。有点什么也不做:-)。为了补充我自己的评论,我认为cron正在收集作业的输出并将其传递给docker监视的stdout。@TomSaleeba,是的,因为重定向是由cron的进程运行的,这就是为什么stdout是cron的唯一输出。我创建了一个repo来解决这个问题:。答案似乎是不添加任何额外的重定向,它将“正常工作”,至少对于我的简单echo
测试是如此。在我的情况下是唯一有效的方法我很好奇。。。为什么要将crontab复制到/tmp,然后复制到/etc/crontab/root?Debian应用了一个补丁来取消标准输出,方法与CentOS类似:我想Alpine是唯一一个没有应用这个补丁的,我建议您在CentOS的答案中添加2>/proc/1/fd/2
,以便捕获发送到stderr的错误
* * * * * echo "Crontab is working - watchdog 1"
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"]
* * * * * echo "Crontab is working - watchdog 1" > /proc/1/fd/1
# 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
* * * * * /run.sh > /tmp/stdout 2> /tmp/stderr