简体   繁体   English

当我启动我的 docker 容器时,Cron 没有运行

[英]Cron isn't running when I start my docker container

To preface, I have been referencing these two articles for help:作为序言,我一直在参考这两篇文章寻求帮助:

My goal is to have a cron job automatically start when I start my docker container.我的目标是在启动 docker 容器时自动启动 cron 作业。 Currently, it doesn't automatically start, but I can manually go into my container and run service cron start , which starts the job, and it works correctly.目前,它不会自动启动,但我可以手动进入我的容器并运行service cron start ,它会启动作业,并且它可以正常工作。

So the problem is: How do I get my cron job to start automatically when my container starts up?所以问题是:当我的容器启动时,如何让我的 cron 作业自动启动?

Dockerfile文件

FROM microsoft/dotnet:latest
RUN apt-get update && apt-get install -y cron

COPY . /app

WORKDIR /app

ADD crontab /etc/cron.d/crontab
RUN chmod 0600 /etc/cron.d/crontab
RUN crontab -u root /etc/cron.d/crontab
RUN touch /var/log/cron.log

RUN ["dotnet", "restore"]

RUN ["dotnet", "build"]

EXPOSE 5000/tcp

CMD cron && tail -f /var/log/cron.log
CMD service cron start

crontab定时任务表

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# Empty space

Though I wasn't able to get cron working in that particular container, I was able to create a standalone docker container specifically for cron, and was successful in getting it to run automatically.虽然我无法让 cron 在那个特定的容器中工作,但我能够专门为 cron 创建一个独立的 docker 容器,并成功地让它自动运行。

As far as setup for the cron container, I followed the linked article, Run a cron job with Docker - Julien Boulay , and was able to get it working.至于 cron 容器的设置,我遵循了链接的文章,使用 Docker 运行 cron 作业 - Julien Boulay ,并且能够让它工作。

What I'm doing is have the CMD call cron directly like this:我正在做的是让 CMD 直接像这样调用 cron:

CMD /usr/sbin/cron -f

Before that I'm adding the crontab to the container and assigning it as the root crontab with the command:在此之前,我将 crontab 添加到容器中,并使用以下命令将其分配为根 crontab:

RUN crontab /root/mycrontab

You don't need to call the crontab command on files that are located in /etc/cron.d , but you do need those files to have the correct syntax.您不需要对位于/etc/cron.d文件调用 crontab 命令,但您确实需要这些文件具有正确的语法。 Using your example, instead of this:使用你的例子,而不是这个:

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1

You should have this:你应该有这个:

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1

On your crontab file.在您的 crontab 文件中。 This only applies to crontab files located within /etc/cron.d , otherwise your crontab file syntax is correct and you use the crontab command to load it.这仅适用于位于/etc/cron.d的 crontab 文件,否则您的 crontab 文件语法是正确的,您可以使用crontab命令加载它。

Starting from your example, I think you should modify your files like this:从你的例子开始,我认为你应该像这样修改你的文件:

Dockerfile文件

FROM microsoft/dotnet:latest
RUN apt-get update && apt-get install -y cron

COPY . /app

WORKDIR /app

ADD crontab /etc/cron.d/crontab
RUN chmod 0600 /etc/cron.d/crontab
RUN touch /var/log/cron.log

RUN ["dotnet", "restore"]

RUN ["dotnet", "build"]

EXPOSE 5000/tcp

CMD /usr/sbin/cron -f

crontab定时任务表

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1

Another alternative would be:另一种选择是:

Dockerfile文件

FROM microsoft/dotnet:latest
RUN apt-get update && apt-get install -y cron

COPY . /app

WORKDIR /app

ADD crontab /root/
RUN crontab /root/crontab
RUN touch /var/log/cron.log

RUN ["dotnet", "restore"]

RUN ["dotnet", "build"]

EXPOSE 5000/tcp

CMD /usr/sbin/cron -f

crontab定时任务表

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1

We had a problem with php-fpm and docker where our cronjob tasks were not be executed.我们在php-fpm和 docker 上遇到了问题,我们的 cronjob 任务没有被执行。 There were two problems we solved:我们解决了两个问题:

  • We tried to copy a crontab file into the docker container by using COPY config/custom-cron /etc/cron.d/custom-cron .我们尝试使用COPY config/custom-cron /etc/cron.d/custom-croncrontab文件复制到 docker 容器中。 The problem is, that our line endings were in windows format.问题是,我们的行尾windows格式的。 This did break our crontab file, because this line endings are not converted while copy that file into the container.这确实破坏了我们的 crontab 文件,因为在将该文件复制到容器时不会转换此行结尾。
  • The second problem was, that we tried to start the cron via CMD ["cron", "-f"] which did block the main php-fpm process.第二个问题是,我们试图通过CMD ["cron", "-f"]启动 cron CMD ["cron", "-f"]这确实阻塞了主要的php-fpm进程。 This results in a 502 Bad gateway error when calling our web application.这会在调用我们的 Web 应用程序时导致502 Bad gateway错误。

Finaly we made it work by editing the crontab file manually while building the docker image instead of copy-pasting and using supervisord to have multiple tasks running inside docker.最后,我们通过手动编辑 crontab 文件来实现它,同时构建 docker 镜像而不是复制粘贴,并使用 supervisord 在 docker 中运行多个任务。 This should work on all supported operating systems.这应该适用于所有支持的操作系统。

dockerfile文件

FROM php:7.1.16-fpm

RUN apt-get update && apt-get install -y cron supervisor

# Configure cron
RUN crontab -l | { cat; echo "* * * * * echo 'Hello world' >> /var/log/cron-test.log 2>&1"; } | crontab -

# Configure supervisor
COPY config/supervisord.conf /etc/supervisor/supervisord.conf

supervisord.conf配置文件

[supervisord]
logfile = /dev/null
loglevel = info
pidfile = /var/run/supervisord.pid
nodaemon = true

[program:php-fpm]
command = php-fpm
autostart = true
autorestart = true
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes = 0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes = 0

[program:cron]
command = cron -f
autostart = true
autorestart = true
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes = 0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes = 0

There is a bug in Debian based distributions which will cause cronjobs to fail because docker uses layered filesystem and cron doesn't start and says NUMBER OF HARD LINKS > 1 (/etc/crontab) .基于 Debian 的发行版中存在一个错误,它会导致 cronjobs 失败,因为 docker 使用分层文件系统并且 cron 无法启动并显示NUMBER OF HARD LINKS > 1 (/etc/crontab)

The fix is simple, add touch /etc/crontab /etc/cron.*/* to the entrypoint of your container.修复很简单,将touch /etc/crontab /etc/cron.*/*到容器的入口点。

I have made a blog post explaining how to setup cron in a Docker container here : https://esc.sh/blog/cron-jobs-in-docker/我在这里发表了一篇博文,解释了如何在 Docker 容器中设置 cron: https : //esc.sh/blog/cron-jobs-in-docker/

I know this is an old question but I found a fix to this on Debian and it solved my problem.我知道这是一个老问题,但我在 Debian 上找到了解决方法,它解决了我的问题。 Cron pam auth with uid was breaking my cron from being able to run.带有 uid 的 Cron pam auth 使我的 cron 无法运行。

RUN sed -i '/session    required     pam_loginuid.so/c\#session    required     pam_loginuid.so/' /etc/pam.d/cron

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM