linux - 我如何在 ubuntu docker 中将 crontab 保存在文件中

标签 linux ubuntu docker cron

我正在使用 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/

相关文章:

macos - 如何增加 docker-machine 内存 Mac

docker - 从Jenkins Docker容器通过SSH进入远程主机

regex - 在 URL 列表中查找域部分中的关键字

linux - 如何找到正在运行的jar文件的路径

docker - Drone - 使用 Drone.yml 自定义日期时间字符串格式命名 docker 图像标签

ubuntu - 如果我在 docker 镜像中运行 sudo apt-get update 并将其作为容器运行,我还需要在机器上运行 sudo apt-get update 吗?

python - 为 Ubuntu 发布

Python 多处理。你如何从 child 那里得到 parent 的地位?

linux - 如何将我的数据拆分成足够小的 block 以提供给 Seq?

linux - 谁在 Linux 中调用 calloc() 时清零页面?