2016-01-23 66 views
1

我使用的码头工人和OS是Ubuntu Linux系统。我怎能的crontab文件在Ubuntu搬运工

如果我使用有crontab -e和地点数据,那么cron的运行正常。

* * * * * /var/www/daily.sh 

但如果去掉容器那么crontab中也不见了。我想以某种方式将crontab放置在某个文件中,例如crontabs.sh,然后将其安装到容器中,以便如果创建容器,那么我的cron仍然存在。

我不知道在什么位置,我需要挂接,这样的cron正常运行。像

/myhost/code/crontabs.sh: /etc/crons.daily

回答

1

正如this answer提到的,您可以复制文件,添加到您的Dockerfile

FROM ubuntu:latest 
MAINTAINER [email protected] 

# 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定义。

可以初始化的“crontab”,本地文件要复制到你的形象,与cronsandbox.com内容。

你的情况:0 23 * * *


如果你不想让每个变化的新形象,为您免除COPY线,并安装该文件在运行时:

docker run -v crontab:/etc/cron.d/hello-cron -n mycontainer myimage 

这样,本地文件crontab安装在容器为/etc/cron.d/hello-cron(或任何你想要的其他名字)。
每当你改变它,停止并重新启动你的容器。

+0

我读过cron.daily在6.30或730Am运行。但我想在11PM运行。 –

+0

此外,我希望cronfile成为源代码的一部分,以便我可以编辑它。如果我把泊坞窗文件,它会很难编辑初始图像后创建 –

+0

@JohnKaff我正在编辑的答案。 cron文件将成为您的资源的一部分。每次修改它时,重建图像。 – VonC