简体   繁体   English

Docker:容器内的 cron 没有错误但仍然无法正常工作

[英]Docker: cron within container no errors but still not working

I am new to docker.我是码头工人的新手。 Managed to create a script which gets info from online source and populates an SQL DB.设法创建一个脚本,该脚本从在线资源获取信息并填充 SQL DB。 All works well within docker container.在 docker 容器中一切正常。

However, I need to make this to run for example every minute.但是,我需要让它每分钟运行一次。

So I amended my working Dockerfile and added the following:所以我修改了我的工作 Dockerfile 并添加了以下内容:

Dockerfile: Dockerfile:

RUN apt-get install -y cron
COPY cronms /etc/cron.d/cronms
RUN chmod 0644 /etc/cron.d/cronms
RUN crontab /etc/cron.d/cronms
RUN touch /var/log/cron.log
    
CMD cron && tail -f /var/log/cron.log

#CMD [ "python3", "./my_script.py" ]  -- command before cron

my cronms file:我的 cronms 文件:

*/1 * * * * /usr/bin/python3 /my_script.py

Image builds without errors however when running it data is not being downloaded.图像构建没有错误,但是在运行它时没有下载数据。

What I am missing please?请问我缺少什么?

Thanks谢谢

Your crontab file has incorrect syntax.您的 crontab 文件的语法不正确。 From thecron man page :cron手册页

The system crontab ( /etc/crontab ) and the packages crontabs ( /etc/cron.d/* ) use the same format, except that the username for the command is specified after the time and date fields and before the command.系统 crontab ( /etc/crontab ) 和包 crontabs ( /etc/cron.d/* ) 使用相同的格式,只是命令的用户名是在时间和日期字段之后和命令之前指定的。

So your cronms file should look like:因此,您的cronms文件应如下所示:

*/1 * * * * root /usr/bin/python3 /my_script.py

I assume the Dockerfile in your question is truncated, but it's worth noting that for this to work you may need to explicitly install python3 as well as cron , depending on which base image you're using.我假设您问题中的Dockerfile已被截断,但值得注意的是,要使其正常工作,您可能需要显式安装python3cron ,具体取决于您使用的基本映像。


If I build an image using this Dockerfile :如果我使用此Dockerfile构建图像:

FROM ubuntu:20.04

RUN apt-get update
RUN apt-get install -y cron python3
COPY cronms /etc/cron.d/cronms
RUN chmod 0644 /etc/cron.d/cronms
RUN crontab /etc/cron.d/cronms
RUN touch /var/log/cron.log

COPY my_script.py /my_script.py

CMD cron && tail -f /var/log/cron.log

And this cronms :而这个cronms

*/1 * * * * root /usr/bin/python3 /my_script.py

And this my_script.py :而这个my_script.py

#!/usr/bin/python3

import time

with open("/tmp/datafile", "a") as fd:
    fd.write(time.ctime())
    fd.write("\n")

Then I can confirm that everything works as expected: the script is executed once a minute.然后我可以确认一切都按预期工作:脚本每分钟执行一次。

Note that nothing is written to /var/log/cron.log , however, because cron logs to syslog, not to a file.但是请注意,没有任何内容写入/var/log/cron.log ,因为cron记录到 syslog,而不是文件。 To see output from cron , you would need to arrange to run a syslog daemon, or use a different cron daemon (for example, the busybox crond command can log to a file or stderr).要查看cron的输出,您需要安排运行 syslog 守护程序,或使用不同的 cron 守护程序(例如,busybox crond命令可以记录到文件或 stderr)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM