如果向 crontab 添加每 10 分钟重复一次的命令,那么第一个作业何时运行? [英] If adding a command that repeats every 10 minutes to crontab, when does the first job run?

查看:17
本文介绍了如果向 crontab 添加每 10 分钟重复一次的命令,那么第一个作业何时运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

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

Mins Hrs Day Mth DoW*/10 * * * *此运行时间 (UTC) 周六 2016 年 1 月 23 日 06532016 年 1 月 23 日星期六 07002016 年 1 月 23 日星期六 07102016 年 1 月 23 日星期六 0720

它使用的语法:

<块引用>

每隔 n 个 '0-23/n','*/2' 将是每隔一个.
*/1"在其他地方通常是可以接受的,但在此处被标记为可能是意外条目.

<小时>

参见例如使用 Docker 运行 cron 作业"(来自 Julien Boulay)

<块引用>

让我们创建一个名为crontab"的新文件来描述我们的工作.

* * * * * root echo "Hello world" >>/var/log/cron.log 2>&1# 对于有效的 cron 文件,此文件末尾需要一个空行.

<块引用>

以下 DockerFile 描述了构建镜像的所有步骤

来自 ubuntu:latest维护人员 docker@ekito.fr# 在cron目录下添加crontab文件添加 crontab/etc/cron.d/hello-cron# 赋予 cron 作业的执行权限运行 chmod 0644/etc/cron.d/hello-cron# 创建能够运行tail的日志文件运行触摸/var/log/cron.log# 在容器启动时运行命令CMD cron &&tail -f/var/log/cron.log

<块引用>

然后你可以用

构建图像

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

<块引用>

并运行它:

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

<块引用>

请耐心等待 2 分钟,您的命令行应显示:

Hello world你好世界

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

As part of the setup of a docker container, the following gets injected into crontab:

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

According to the behavior of crontab, when should the first run kick off? Should the 10 minute cycle begin instantly, or 10 minutes after this is put into crontab. Neither behavior is happening so I am trying to debug this in more depth by trying to understand the intended behavior.

解决方案

This cron sandbox simulator gives you an idea:

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

It uses the syntax:

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


See for example "Run a cron job with Docker" (by 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

If you replaced the first '' by '/10', you would have to wait to the next 0 or 10 or 20 or... of the hour.

这篇关于如果向 crontab 添加每 10 分钟重复一次的命令,那么第一个作业何时运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆