我正在使用 docker,操作系统是 Ubuntu。
如果我使用 crontab -e
并在其中放置数据,那么 cron 运行良好。
* * * * * /var/www/daily.sh
但是,如果删除容器,那么该 crontab 也将消失。我想以某种方式将 crontab 放在一些文件中,例如 crontabs.sh
,然后将其安装在容器中,这样如果我创建容器,那么我的 cron 仍然存在。
我不知道我需要将它安装在什么位置,以便 cron 正常运行。像
/myhost/code/crontabs.sh:/etc/crons.daily
最佳答案
如 this answer 中所述,你可以复制你的文件,添加到你的Dockerfile
:
FROM ubuntu:latest
MAINTAINER docker@ekito.fr
# Add crontab file in the cron directory
COPY crontab /etc/cron.d/crons.daily
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
(来源:示例“Run a cron job with Docker”(来自 Julien Boulay)
这样,您的镜像将始终包含正确的 cron 定义。
您可以使用cronsandbox.com 初始化'crontab
' 的内容,这是您正在复制到图像的本地文件。 .
在你的情况下:0 23 * * *
如果您不想在每次更改时都制作一个新图像,您可以删除 COPY
行,并在运行时挂载该文件:
docker run -v crontab:/etc/cron.d/hello-cron -n mycontainer myimage
这样,本地文件 crontab
就像在容器中一样挂载为 /etc/cron.d/hello-cron
(或您想要的任何其他名称)。
每当您更改它时,停止并重新启动您的容器。
关于linux - 我如何在 ubuntu docker 中将 crontab 保存在文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34964737/