Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何在Ubuntu Docker容器中启动cron plus Shell脚本_Bash_Docker_Cron - Fatal编程技术网

Bash 如何在Ubuntu Docker容器中启动cron plus Shell脚本

Bash 如何在Ubuntu Docker容器中启动cron plus Shell脚本,bash,docker,cron,Bash,Docker,Cron,我试图在Ubuntu20.10(Groovy Gorilla)Docker容器中自动启动cron,但迄今为止没有成功 从前面的搜索()中,我发现了一个使用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 dire

我试图在Ubuntu20.10(Groovy Gorilla)Docker容器中自动启动cron,但迄今为止没有成功

从前面的搜索()中,我发现了一个使用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
但是,我的服务器设置无法实现这一点。稍后,我在Dockerfile中还有另一个CMD(类似于):

当然,只会运行第二个CMD。我已尝试组合多个命令:

CMD cron && tail -f /var/log/cron.log && /usr/sbin/run-lamp.sh
但这不会运行run-lamp.sh。我还尝试将命令放在run-lamp.sh中,但没有任何结果导致cron启动。话虽如此,通过在容器中打开一个shell并输入以下内容,手动启动cron非常容易:

# cron
# crontab /etc/cron.d/cronfile
我愿意听取建议

我正在使用的所有文件都可以在此处找到:

https://github.com/Downes/gRSShopper
特别是: Dockerfile:

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

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

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

提前感谢。

首先,你不需要那种
尾部-f/var/log/cron.log
,它在容器中是无用的

其次,
tail-f
被设计为仅在收到信号时停止,而您从未发出信号,因此它不会停止,因此下一个命令
run lamp.sh
将不会运行

这是一个最小的复制器:

entrypoint.sh

#!/bin/bash

touch /tmp/x
sleep 120
cronfile

*   *   *   *   *   touch /tmp/y
# An empty line is required at the end of this file for a valid cron file.
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
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
Dockerfile

*   *   *   *   *   touch /tmp/y
# An empty line is required at the end of this file for a valid cron file.
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
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
测试命令:

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"
结果:

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
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
但是,使用此
Dockerfile

*   *   *   *   *   touch /tmp/y
# An empty line is required at the end of this file for a valid cron file.
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
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
结果:

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

通常,您希望每个容器只运行一个进程,在本例中,这意味着运行一个单独的容器,只运行一个前台cron守护进程。这个设置对你有用吗?这就是我现在正在调查的。