Cron和Crontab文件在Docker中没有执行

11

我有这个简单的Dockerfile用于测试,但在我的LEMP堆栈中的PHP镜像中也是同样的问题:cron作业在Docker中不被执行。

这是我的测试Dockerfile:

FROM debian:latest
MAINTAINER XY <info@domain.com>
LABEL Description="Cron" Vendor="Istvan Lantos" Version="1.0"

RUN apt-get -y update && apt-get -y dist-upgrade \
    && apt-get -y install \
        cron \
        rsyslog \
        vim

RUN rm -rf /var/lib/apt/lists/*

#cron fixes
RUN touch /etc/crontab /etc/cron.d/* /var/spool/cron/crontabs/*
#COPY etc/cron.d /etc/cron.d
COPY etc/crontab /etc/crontab
#COPY var/spool/cron/crontabs /var/spool/cron/crontabs
RUN chmod 600 /etc/crontab /etc/cron.d/* /var/spool/cron/crontabs/*
RUN touch /etc/crontab /etc/cron.d/* /var/spool/cron/crontabs/*

RUN rm -rf /var/lib/apt/lists/*

COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
CMD ["/docker-entrypoint.sh"]

docker-entrypoint.sh:

#!/bin/bash
set -e

echo PID1 > /dev/null

/etc/init.d/rsyslog start

#Stay in foreground mode, don’t daemonize.
/usr/sbin/cron -f

这是Crontab文件。我还在/etc/cron.d/var/spool/cron/crontabs中放置了一行代码,并使用用户的名称,但效果与修改此基本crontab文件相同:cron作业不会被执行。
MAILTO=""
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/php7/bin:/usr/local/php7/sbin

# m h dom mon dow user  command
#17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
#25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
#47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
#52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
*/1 *   * * *   root    date >> /var/log/cron-test.log 2>&1

这是/var/log/syslog文件的输出内容:
Jan 23 09:38:39 1ab854e8d9a7 rsyslogd: [origin software="rsyslogd" swVersion="8.4.2" x-pid="14" x-info="http://www.rsyslog.com"] start
Jan 23 09:38:39 1ab854e8d9a7 rsyslogd: imklog: cannot open kernel log(/proc/kmsg): Operation not permitted.
Jan 23 09:38:39 1ab854e8d9a7 rsyslogd-2145: activation of module imklog failed [try http://www.rsyslog.com/e/2145 ]
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (CRON) INFO (pidfile fd = 3)
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (*system*) NUMBER OF HARD LINKS > 1 (/etc/crontab)
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (*) ORPHAN (no passwd entry)
Jan 23 09:38:39 1ab854e8d9a7 cron[19]: (CRON) INFO (Running @reboot jobs)

这个cron任务不会创建/var/log/cron-test.log文件。

我对那些认为这是“离题”和适合SuperUser材料的人提出了问题,而且这是关于一般计算机硬件和软件的:真的吗?当Docker问题成为系统管理员的事情时?这样每个与Docker相关的问题都会在这里至少有一个标记。我不反对将更多的用户推广到较少人知道的子网站上,但我们在这里得到答案的机会更大。

更新:

在cron作业无法工作之前,这是我想到的:

Dockerfile结尾:

COPY cron-jobs.sh /
RUN chmod +x /cron-jobs.sh

COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
CMD ["/docker-entrypoint.sh"]

docker-entrypoint.sh:

#!/bin/bash
set -e

echo PID1 > /dev/null

# Run script in the background (this is not daemonized)
/cron-jobs.sh &

/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf

cron-jobs.sh:

#!/bin/bash

while true; do
  date >> /var/log/cron-test.log 2>&1
  sleep 60
done

1
今天早上我提到的例子在这里适用吗?(https://dev59.com/BZPfa4cB1Zd3GeqPBlEZ#34960575) - VonC
1
你试过使用baseimage-docker吗?它内置了syslog和cron。不过是Ubuntu系统。 - Simon Thum
@VonC 我尝试了使用 * * * * * root echo "Hello world" >> /var/log/cron.log 2>&1 的示例和一个带有 # 的空行。虽然 cron.log 被创建了,但在最初的5-7分钟后它没有被填充上 Hello World(示例说应该每2分钟回显一次?)。syslog 显示 (*system*hello-cron) NUMBER OF HARD LINKS > 1 (/etc/cron.d/hello-cron)。老实说,我很满意 bash 脚本解决方案,但还是想知道为什么不起作用。如果可能的话,我想坚持使用 Debian。 - Lanti
1
  • 表示每分钟,*/2 表示在 0、2、4 等时刻执行。空行表示为空(不以 # 开头的空行)。
- VonC
1个回答

27

在Debian中,Cron不会执行具有超过1个硬链接的crontabs,请参阅bug 647193。由于Docker使用覆盖,它会导致文件具有多个链接,因此您需要在启动脚本中使用touch命令,以便断开链接:

touch /etc/crontab /etc/cron.*/*

2
我刚刚花了3个小时在谷歌上搜索这个问题,最终找到了一个解释得很好的答案和启动命令来解决整个问题。谢谢。 - Nahydrin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接