首页 > 解决方案 > Docker Container:Cronjobs 未启动(确定我的错误)

问题描述

我正在利用当前的时间来发展自己,我决定在 Docker Containers 上投入一些时间。

我为自己创建了一些我现在正在工作的小型学习练习。

我的 Cronjobs 有问题。它应该是每分钟运行的 cronjob。已完成脚本的运行时间约为 40-50 秒。因此我决定不每分钟启动一个容器,我想保持容器运行并在容器内运行 cronjobs。

我的问题:Cronjob 没有启动。

#Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get -yq install cron
COPY scripts/cmd.txt /home/cmd.sh
RUN chmod 744 /home/cmd.sh
COPY scripts/cron.txt /etc/cron.d/test-cron
RUN chmod 644 /etc/cron.d/test-cron
RUN crontab /etc/crontab.d/test-cron
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log

# scripts/cmd.txt
echo "test aus CMD" >> /var/log/cron.log

# scripts/cron.txt
* * * * * root /home/cmd.sh >> /var/log/cron.log 2>&1
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1

据我了解,/var/log/cron.log 应该会随着时间的推移获得一些内容,但没有任何反应。我很确定是我造成了这个错误,但我没有找到它。

有人知道我做错了什么吗?

谢谢和亲切的问候霍尔格

标签: linuxdockercrondockerfile

解决方案


您的文件中有一些拼写错误。

#Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get -yq install cron
COPY scripts/cmd.txt /home/cmd.sh
RUN chmod 744 /home/cmd.sh
COPY scripts/cron.txt /etc/cron.d/test-cron
RUN chmod 644 /etc/cron.d/test-cron
RUN crontab /etc/crontab.d/test-cron # Here it is, just replace `crontab.d` by `cron.d`
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log

# scripts/cron.txt
* * * * * root /home/cmd.sh >> /var/log/cron.log 2>&1 # Remove the `root` command from this line...
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1 # ...and also from that one

推荐阅读