[英]cron job not running inside docker container on ubuntu
我有一个简单的Dockerfile如下
FROM ubuntu:latest
ADD crontab /etc/cron.d/test-cron
RUN chmod a+x /etc/cron.d/test-cron
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log
和crontab
文件的内容一样简单
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# empty line
当我在我的本地OS X机器上运行它(运行docker-machine)时,它工作正常(“Hello world”每分钟打印到日志文件)。 但是,当我尝试在Ubuntu机器上运行它时,cron作业不会运行(空日志文件)。
这是我用来运行容器的命令
docker build -t crontest .
docker run --name cron crontest
我不知道为什么会这样。 我想知道我的Ubuntu盒子是否有问题(错误的时间设置?)。 我试图重启该机器无效。 我目前在Ubuntu盒子上运行其他docker容器,它们运行正常。
关于如何调试/修复此问题的任何建议都将非常受欢迎。
编辑:
进入容器( docker exec -it cron /bin/bash
)后,我可以验证cron是否在那里运行:
root@a2ad451af8d9:/# ps -ef | grep cron
root 1 0 0 20:15 ? 00:00:00 /bin/sh -c cron && tail -f /var/log/cron.log
root 6 1 0 20:15 ? 00:00:00 cron
root 7 1 0 20:15 ? 00:00:00 tail -f /var/log/cron.log
root 25 11 0 20:21 ? 00:00:00 grep --color=auto cron
使用apt-get install rsyslog
rsyslogd
在容器内apt-get install rsyslog
rsyslogd
在使用cron -L15
(最大日志记录)启动cron之前使用命令rsyslogd
启动它。 然后查看容器内的文件/var/log/syslog
以查看cron守护程序自己的日志输出。 它将告诉您解析crontab时是否存在问题,在您的情况下,如果已注册并且正在尝试运行您的作业,则会在每分钟记录一个与下面类似的条目。
CRON[16]: (root) CMD (echo "Hello world" >> /var/log/cron.log 2>&1)
我有类似的问题,特别是Ubuntu 14.04。 为了调试,我尝试在前台运行cron,并发现它在尝试运行预定作业时发出System error
消息。
显然,它是一个已知的问题--net=host
parameter(ref: https : //github.com/moby/moby/issues/5899 )。 我按照建议尝试传递--pid=host
,然后,cron作业开始运行正常。
我有一个名为backup.sh
的备份脚本,我将其复制到/etc/cron.daily
。 该脚本未正确调用。
要做到这一点,我不得不重命名为只backup
没有.sh
所以对我来说ls -l /etc/cron.daily
有以下输出:
root@0989a35b8f94:/# ls -l /etc/cron.daily
total 24
-rwxr-xr-x 1 root root 1474 Sep 13 16:47 apt-compat
-rwxrwxr-x 1 root root 45 Nov 9 11:18 dobackup
-rwxr-xr-x 1 root root 1597 Feb 22 2017 dpkg
-rwxr-xr-x 1 root root 4125 Mar 2 2016 exim4-base
-rwxr-xr-x 1 root root 249 May 17 11:59 passwd
为了测试/分析这个,我使用了以下方法:
我查找了crontab文件cat /etc/crontab
显示了每日cronjobs的以下行:
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
然后我将我的备份脚本隔离到一个专用文件夹中:
mkdir /etc/cron.test
mv /etc/cron.daily/dobackup /etc/cron.test
然后跑步
test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.test)
并在另一个终端上使用ps auxf
向我展示了现在正在运行的作业。 您还可以在重命名为.sh
版本时验证它是否中断:
mv /etc/cron.test/dobackup /etc/cron.test/dobackup.sh
test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.test)
然后它立即存在,因此没有工作。
在我的情况下,当我COPY
文件时,它创建了一个辅助硬链接(它可能是Windows的东西)。
我要做的是确保文件是在正在运行的容器中创建的。
FROM mysql:5.7
RUN apt-get update && apt-get install -y anacron
COPY crontab /tmp/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD (cat /tmp/crontab > /etc/cron.d/hello-cron ) && cron && tail -f /var/log/cron.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.