Cron和Crontab文件未在Docker中执行 [英] Cron and Crontab files not executed in Docker

查看:1939
本文介绍了Cron和Crontab文件未在Docker中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的Dockerfile测试,但是在PHP镜像中的LEMP堆栈也是一样的:cron作业根本不是在Docker中执行。



是我的测试Dockerfile:

  FROM debian:latest 
MAINTAINER XY< info@domain.com>
LABEL描述=CronVendor =Istvan LantosVersion =1.0

运行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

#在前台模式下停留,不进行守护进程。
/ usr / sbin / cron -f

这是Crontab文件。我还在 /etc/cron.d / var / spool / cron / crontabs 中放置了一个的用户,但效果是一样的,就像我修改了这个基本的crontab文件:cron工作不会被执行:

  MAILTO =
#/ etc / crontab:system-wide crontab
#与任何其他crontab不同,您不必运行`crontab'
#命令来安装新版本当你编辑这个文件
#和/etc/cron.d中的文件。这些文件还有用户名字段,
#,没有其他的crontab做。

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 / php7 / sbin

#mh dom dom用户命令
#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 * * * *根日期> /var/log/cron-test.log 2>& 1

/ var / log / syslog 档案:

  Jan 23 09:38 :39 1ab854e8d9a7 rsyslogd:[origin software =rsyslogdswVersion =8.4.2x-pid =14x-info =http://www.rsyslog.com] start 
Jan 23 09 :38:39 1ab854e8d9a7 rsyslogd:imklog:无法打开内核日志(/ proc / kmsg):不允许操作。
Jan 23 09:38:39 1ab854e8d9a7 rsyslogd-2145:模块imklog的激活失败[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 *)硬链接数> 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(运行@reboot作业)

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



我有一个问题,谁标记为 和超级用户材料,加上这是关于一般计算硬件和软件:真的吗? Docker问题当成为sysadmin的东西?这样每个Docker相关的问题在这里至少有一个标志。我不反对将更多用户推荐给不太知名的儿童网站,但是我们有更多的变化来获得这里的答案。



更新: strong>



这是我想出的,直到cron工作无效:



Dockerfile结束: / p>

  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

#在后台运行脚本(这不是守护进程的)
/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


错误647193 。由于Docker使用覆盖,它会导致多个链接到文件,因此您必须在启动脚本中触摸,因此链接被切断:

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


I have this simple Dockerfile for testing, but this is also same in my LEMP stack in a PHP image: cron jobs simply not being executed in Docker.

This is my testing 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

And this is the Crontab file. I also placed one liners in /etc/cron.d or /var/spool/cron/crontabs with the name of the user, but the effect was the same just like if I modified this base crontab file: cron jobs not will be executed:

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

This is the output of the /var/log/syslog file:

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)

/var/log/cron-test.log won't be created by the cron job.

I have a question for those who flagged this as "off topic" and SuperUser material, plus this is about general computing HARDWARE AND SOFTWARE: really? Docker questions when become sysadmin stuff? This way every Docker related question here have at least one flag. I'm not against promoting more users to the less known child sites, but we have more change to get the answer here than their.

UPDATE:

This is what I come up with until cron jobs not working:

End of 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

解决方案

Cron (at least in Debian) does not execute crontabs with more than 1 hardlink, see bug 647193. As Docker uses overlays, it results with more than one link to the file, so you have to touch it in your startup script, so the link is severed:

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

这篇关于Cron和Crontab文件未在Docker中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆