Cron和Crontab文件未在Docker中执行

Lan*_*nti 7 cron crontab docker dockerfile docker-compose

我有这个简单的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"]
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

这是/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)
Run Code Online (Sandbox Code Playgroud)

/var/log/cron-test.log 将不会由cron作业创建.

对于那些将此标记为"非主题"和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"]
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

cron-jobs.sh:

#!/bin/bash

while true; do
  date >> /var/log/cron-test.log 2>&1
  sleep 60
done
Run Code Online (Sandbox Code Playgroud)

Erb*_*ica 23

Cron(至少在Debian中)不会执行超过1个硬链接的crontabs,请参阅错误647193.由于Docker使用叠加层,因此它会生成多个指向该文件的链接,因此您必须touch在启动脚本中使用它,因此链接会被切断:

touch /etc/crontab /etc/cron.*/*
Run Code Online (Sandbox Code Playgroud)

  • 我花了3个小时在这个问题上进行了调查,终于找到了一个解释清楚的答案和启动命令,以解决整个问题。谢谢。 (2认同)