linux - 如果将每 10 分钟重复一次的命令添加到 crontab,第一个作业何时运行?

标签 linux docker cron

作为 docker 容器设置的一部分,以下内容被注入(inject)到 crontab 中:

*/10 * * * * /opt/run.sh >> /opt/run_log.log

根据crontab的行为,第一次运行应该在什么时候开始? 10 分钟的周期应该立即开始,还是在将其放入 crontab 后 10 分钟。这两种行为都没有发生,所以我试图通过理解预期的行为来更深入地调试它。

最佳答案

这个 cron 沙盒模拟器给你一个想法:

Mins    Hrs Day Mth DoW
*/10    *   *   *   *

This run time (UTC)     Sat 2016-Jan-23 0653
Forward Schedule    Sat 2016-Jan-23 0700
    Sat 2016-Jan-23 0710
    Sat 2016-Jan-23 0720

它使用语法:

Every nth '0-23/n', '*/2' would be every other.
'*/1' is generally acceptable elsewhere, but is flagged here as possibly an unintended entry.


例如参见“Run a cron job with Docker”(作者 Julien Boulay)

Let’s create a new file called “crontab” to describe our job.

* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.

The following DockerFile describes all the steps to build your image

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

# 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

# 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

Then you can build the image with

sudo docker build --rm -t ekito/cron-example .

And run it:

sudo docker run -t -i ekito/cron-example

Be patient, wait for 2 minutes and your commandline should display:

Hello world
Hello world

如果您将第一个“”替换为“/10”,您将不得不等待下一个小时的 0 或 10 或 20 或...。

关于linux - 如果将每 10 分钟重复一次的命令添加到 crontab,第一个作业何时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34957338/

相关文章:

php - 如何确定 cronjob 中的环境

linux - 为什么 more 命令无法读取标准输入,而是从管道标准输入读取?

linux - 使用 xorg 在 Linux 下的 FreeGLUT 中共享上下文

docker - Kubernetes 服务无法通过具有桌面 Docker 设置的节点端口访问

node.js - 如何基于 GitHub webhook 自动化部署 docker?

selenium - 如何将文件上传到在 docker 中运行的 selenium 测试

python - crontab 不执行 python 脚本

linux - 使用交互式响应动态响应期望

linux - 将 tcp_max_tw_buckets 设置为非常小的值有什么副作用?

cron - 如何设置 Cronjob 以在 Docker 内重新索引 Magento 2