docker cron 不工作

Sha*_*tar 4 linux ubuntu containers docker

我正在尝试构建一个包含 cron 的 docker 镜像。从 docker 文件系统的特定位置删除文件的 cron。下面是我的 Dockerfile

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

RUN apt-get update && apt-get -y install cron

# Copy testfiles folder to docker container.
COPY ./testfiles /opt/

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

RUN (crontab -l -u root; echo "* * * * * root rm -rf /opt/*") | crontab

# Run the command on container startup
CMD cron

ENTRYPOINT ["/bin/sh", "-c", "/bin/bash"]
Run Code Online (Sandbox Code Playgroud)

一切都很成功。我的 cron 也设置在容器中

roadrunner:test shailesh$ docker run -it crontest /bin/bash
root@ac31f5acc49f:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@ac31f5acc49f:/# crontab -l
* * * * * root rm -rf /opt/*
root@ac31f5acc49f:/# cd /opt/
root@ac31f5acc49f:/opt# ls  
file1  file10  file11  file12  file13  file14  file15  file16  file17  file18  file19  file2  file20  file21  file22  file23  file24  file25  file3  file4  file5  file6  file7  file8  file9
Run Code Online (Sandbox Code Playgroud)

但是它没有运行并删除文件/opt/夹中的文件。有人能告诉我配置有什么问题吗?

小智 10

尝试这样的事情,

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

RUN apt-get update && apt-get -y install cron

# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /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 Code Online (Sandbox Code Playgroud)

创建文件 crontab 并添加这样的条目

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
Run Code Online (Sandbox Code Playgroud)

希望能帮到你!!!

  • 感谢您的回答,但这没有帮助。它没有在该位置“/var/log/cron.log 2>&1”中创建日志条目 (2认同)