Crontab,每 6+1 小时(不是每 7 小时)

Crontab, every 6+1 hours (not every 7 hours)

如果我将我的 cron 设置为:

* */6 * * *

那么我的理解是,它会运行在每一天每个被6整除的小时,所以:

06:00
12:00
18:00
24:00

假设我希望它 运行 它延迟 1 小时 [伪代码]:

* */6+1 * * *

06:00 + 01:00 = 07:00
12:00 + 01:00 = 13:00
18:00 + 01:00 = 19:00
24:00 + 01:00 = 01:00

我该怎么做?

如果我做到了*/7:

* */7 * * *

会运行:

07:00
14:00
21:00

这不是我要找的。

cron 有几种不同的实现方式,因此请通过 运行 man 5 crontab 查看系统手册以确认其支持的格式。

假设您有流行的“Vixie cron”,格式说明包括以下内容:

Step values can be used in conjunction with ranges. Following a range with /<number> specifies skips of the number's value through the range. For example, 0-23/2 can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is 0,2,4,6,8,10,12,14,16,18,20,22). Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.

以上:

A field may be an asterisk (*), which always stands for first-last.

所以,对于 0-23/6*/6 实际上是 short-hand - 从 0 开始,并以 6 步继续,直到我们到达 23,给出0,6,12,18。 (请注意,小时字段的范围是 0 到 23,而不是 1 到 24,正如您在问题中暗示的那样。)

因此,在此实现中,序列 1,7,13,19 可以写成 1-23/6(或 1-19/6)。

跳过较大的部分,完整地写出列表 (1,7,13,19) 可以说更具可读性,并且应该适用于 cron 的所有版本,但这取决于您。