CRON触发多快? [英] How quickly is CRON triggered?

查看:182
本文介绍了CRON触发多快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有CRON工作

 30 2 * * * ....

然后它将在每次2:30时运行

Then this would run every time when it is 2:30 at night (local time).

现在假设我的时区为欧洲/德国,现在是2017-10 -29(切换夏令时的日期)。那么此CRON作业将运行两次,对吧?

Now suppose I have the time zone Europe/Germany and it's 2017-10-29 (the day when DST is switched). Then this CRON job would run twice, right?

假设我的时区为欧洲/德国和CRON作业

Suppose I have the time zone Europe/Germany and the CRON job

 30 11 * * * ....

由于德国从未在11:30更改夏令时,因此这不会受到干扰。但是用户可以更改本地时间。要非常清楚:这个问题与DST无关。

As Germany never had a DST change at 11:30, this will not interfere. But the user could change the local time. To be super clear: This question is NOT about DST.

对于以下测试用例,我想知道是否/多久计划CRON作业:

For the following test cases, I would like to know if/how often the CRON job gets scheduled:


  1. 在11:29:58.0,用户将时间设置为11:31:00

  2. 在11:29:59.1,用户将时间设置为11:31:00

  3. 在11:29:59.6,用户将时间设置为11: 31:00

  4. 在11:30:01.0,用户将时间设置为11:29:59.7-之后会直接执行CRON吗?

  1. At 11:29:58.0, the user sets the time to 11:31:00
  2. At 11:29:59.1, the user sets the time to 11:31:00
  3. At 11:29:59.6, the user sets the time to 11:31:00
  4. At 11:30:01.0, the user sets the time to 11:29:59.7 - is CRON executed directly afterwards?

他们归结为 CRON触发的速度有多快?,第4位还提出了一个问题,即CRON是否存储了该分钟已被执行的问题。

They boil down to How quickly is CRON triggered?, where the 4th one also has the question if CRON stores that it was already executed for that minute.

同一问题的另一个变体:

Another variant of the same question:


  1. 在11:29:59,NTP服务将时间更正为11:31:00-该作业将在当天全部执行吗?


推荐答案

最有信心地回答这个问题的最简单方法是看一下cron守护程序的源代码。有一些在线版本,例如,或者您可以使用 apt-get source cron

The easiest way to answer this with confidence is to take a look at the source for the cron daemon. There are a few versions online like this, or you can use apt-get source cron.

cron中的滴答周期是反复睡眠一分钟,如果有工作要做,则该睡眠时间或更短。从睡眠中恢复后,立即检查时间并将结果视为以下 wakeupKind 值之一:

The tick cycle in cron is to repeatedly sleep for a minute, or less if there is a job coming up. Immediately after emerging from the sleep, it checks the time and treats the result as one of these wakeupKind values:


  • 期望的时间-运行我们期望的所有工作

  • 小跃进(最多5分钟)-在中间的几分钟内运行工作

  • 介质向前跳转(最多3个小时,因此这将包括从春季开始的DST)-首先运行任何通配符作业(因为追赶可能需要一分钟以上的时间),然后赶上介于中间的固定时间作业

  • 大跳(两种方式都需要3小时或更长时间)-从当前时间重新开始

  • 向后跳(最多3小时,因此包括DST结束)-因为任何固定时间的作业都已经可能已经运行,所以只能运行任何通配符作业,直到再次捕获时间为止。

  • Expected time - run any jobs we were expecting
  • Small jump forwards (up to 5 minutes) - run the jobs for the intervening minutes
  • Medium jump forwards (up to 3 hours, so this would include DST starting in spring) - run any wildcard jobs first (because the catch up could take more than a minute), then catch up on the intervening fixed time jobs
  • Large jump (3 hours or more either way) - start over with the current time
  • Jump backwards (up to 3 hours, so including the end of DST) - because any fixed time jobs have 'probably' already run, only run any wildcard jobs until the time is caught up again

如果有疑问,消息来源会评论 wakeupKind v

If in doubt, the source comments these wakeupKind values clearly.

编辑

要跟踪 sleep()是否可能受时钟变化的影响,似乎答案是在几个Linux人中间接存在的页面。

To follow up on whether sleep() could be affected by a clock change, it looks like the answer is indirectly there in a couple of the Linux man pages.

  • Firstly the notes for the sleep() function confirm that is implemented by nanosleep()
  • The notes for nanosleep() say Linux measures the time using the CLOCK_MONOTONIC clock (even though POSIX.1 says it shouldn't)
  • Scroll down a bit in the docs for clock_settime() to see the explanation of CLOCK_MONOTONIC, which explains it is not affected by jumps in the system time, but it would be affected by incremental NTP style clock sync adjustments.

因此,总而言之,系统管理员风格时钟更改不会影响 sleep()。但是例如,如果进行了NTP调整,并说轻轻地提前了时钟,则cron将经历一系列稍短的 sleep()函数调用。

So in summary, a system admin style clock change will have no effect on the sleep(). But for example if an NTP adjustment came in and said to 'gently' advance the clock, cron would experience a series of slightly short sleep() function calls.

这篇关于CRON触发多快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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