简体   繁体   English

如何在 Ubuntu Docker 容器中启动 cron 和 shelll 脚本

[英]How to start cron plus a shelll script in an Ubuntu Docker container

I am attempting to start cron automatically in an Ubuntu 20.10 (Groovy Gorilla) Docker container, thus far without success.我试图在 Ubuntu 20.10 (Groovy Gorilla) Docker 容器中自动启动 cron,但到目前为止没有成功。

From previous searches ( example ), I've found a method to start cron using Dockerfile as follows:从之前的搜索( 示例)中,我找到了一种使用 Dockerfile 启动 cron 的方法,如下所示:

# Install and enable cron
RUN apt-get install systemd -y
RUN apt-get install cron -y
RUN systemctl enable cron

# Copy cron file to the cron.d directory
COPY cronfile /etc/cron.d/cronfile

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cronfile

# Apply cron job
RUN crontab /etc/cron.d/cronfile

# Create the log file to be able to run tail
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log

However, I can't make this work with my server setup.但是,我无法使用我的服务器设置进行这项工作。 I have another, later, CMD in my Dockerfile (similar to this ):后来,我的 Dockerfile 中有另一个 CMD(类似于):

CMD ["/usr/sbin/run-lamp.sh"]

and of course only the second CMD will be run.当然只有第二个 CMD 会运行。 I have tried combining multiple commands:我尝试过组合多个命令:

CMD cron && tail -f /var/log/cron.log && /usr/sbin/run-lamp.sh

but this does not run run-lamp.sh.但这不会运行 run-lamp.sh。 I also tried putting the commands inside run-lamp.sh, but nothing has resulted in cron starting.我还尝试将命令放在 run-lamp.sh 中,但没有任何结果导致 cron 启动。 Having said that, it is very easy to start cron manually, by opening up a shell in the container and entering the following:话虽如此,手动启动 cron 非常容易,只需在容器中打开 shell 并输入以下内容:

# cron
# crontab /etc/cron.d/cronfile

I am open to suggestions.我愿意接受建议。

All the files I'm working with are available here:我正在使用的所有文件都可以在这里找到:

https://github.com/Downes/gRSShopper

In particular: Dockerfile:特别是:Dockerfile:

https://github.com/Downes/gRSShopper/blob/master/Dockerfile

run-lamp.sh:运行灯.sh:

https://github.com/Downes/gRSShopper/blob/master/run-lamp.sh

cronfile: cron 文件:

https://github.com/Downes/gRSShopper/blob/master/cronfile

Thanks in advance.提前致谢。

First off, you don't need that tail -f /var/log/cron.log , it's useless in a container.首先,您不需要那个tail -f /var/log/cron.log ,它在容器中没用。

Secondly, tail -f is designed to only stop on a signal, and you never signal it, so it will not stop, and therefore the next command, run-lamp.sh , will not run.其次, tail -f设计为仅在收到信号时停止,而您从不发出信号,因此它不会停止,因此下一个命令run-lamp.sh将不会运行。

Here's a minimal reproducer:这是一个最小的复制器:

entrypoint.sh : entrypoint.sh

#!/bin/bash

touch /tmp/x
sleep 120

cronfile : cronfile

*   *   *   *   *   touch /tmp/y
# An empty line is required at the end of this file for a valid cron file.

Dockerfile : Dockerfile

FROM ubuntu:20.10

RUN apt-get update
RUN apt-get install systemd -y
RUN apt-get install cron -y
RUN systemctl enable cron

# Copy cron file to the cron.d directory
COPY cronfile /etc/cron.d/cronfile

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cronfile

# Apply cron job
RUN crontab /etc/cron.d/cronfile

# Create the log file to be able to run tail
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log

COPY entrypoint.sh /entrypoint.sh
CMD /entrypoint.sh

Test command:测试命令:

docker build -t lamp . \
    && docker rm -f lamp \
    && docker run -d --name lamp lamp \
    && echo waiting for cron... \
    && sleep 61 \
    && docker exec lamp ls /tmp \
    && docker exec lamp sh -c "ps -e | grep cron || echo no cron"

Result:结果:

Sending build context to Docker daemon  71.17kB
Step 1/12 : FROM ubuntu:20.10
...
Successfully tagged lamp:latest
lamp
0fbe19e0583b178543ccf1d1108f72b7f3f6dffb664122621bc67d5939b66672
waiting for cron...
x
no cron

However, with this Dockerfile :但是,有了这个Dockerfile

FROM ubuntu:20.10

RUN apt-get update
RUN apt-get install systemd -y
RUN apt-get install cron -y
RUN systemctl enable cron

# Copy cron file to the cron.d directory
COPY cronfile /etc/cron.d/cronfile

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cronfile

# Apply cron job
RUN crontab /etc/cron.d/cronfile

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

COPY entrypoint.sh /entrypoint.sh
# Run everything in parallel with '&', even the useless tail command
CMD /entrypoint.sh & cron & tail -f /var/log/cron.log

Result:结果:

Sending build context to Docker daemon  87.55kB
Step 1/11 : FROM ubuntu:20.10
...
Successfully tagged lamp:latest
lamp
99dca45fe135326ca96ea90fe21ff7ae23689a56fab5cf0c2ccd8252bc4be84a
waiting for cron...
x
y
     10 ?        00:00:00 cron

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

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