且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每 N 分钟加上偏移量运行一次 Cron 作业

更新时间:2022-12-26 13:18:58

要从每小时 5 点开始每 20 分钟运行一次任务,试试这个:

To run a task every 20 minutes starting at 5 past the hour, try this:

 5-59/20 * * * *

说明

分钟字段中的 *0-59/1 相同,其中 0-59range1步骤.该命令将在 range (0) 的第一分钟运行,然后在距离第一分钟 step (1) 的所有连续分钟内运行,直到最后一个 (59).

An * in the minute field is the same as 0-59/1 where 0-59 is the range and 1 is the step. The command will run at the first minute in the range (0), then at all successive minutes that are distant from the first by step (1), until the last (59).

这就是为什么 */20 * * * * 将在 0 分钟、20 分钟后和 40 分钟后运行 - 这与每 20 分钟相同.但是,*/25 * * * * 将在 0 分钟、25 分钟后和 50 分钟后运行——这与每 25 分钟不同.这就是为什么通常希望在分钟字段中使用一个步长值,将其平均分为 60.

Which is why */20 * * * * will run at 0 minutes, 20 minutes after, and 40 minutes after -- which is the same as every 20 minutes. However, */25 * * * * will run at 0 minutes, 25 minutes after, and 50 minutes after -- which is not the same as every 25 minutes. That's why it's usually desirable to use a step value in the minute field that divides evenly into 60.

因此要偏移开始时间,请明确指定范围并将第一个值设置为偏移量.

So to offset the start time, specify the range explicitly and set the first value to the amount of the offset.

示例

5-59/20 * * * * 将在 5 分钟后、25 分钟后和 45 分钟后运行.

5-59/20 * * * * will run at 5 minutes after, 25 minutes after, and 45 minutes after.

10-59/25 * * * * 将在 10 分钟后和 35 分钟后运行.

10-59/25 * * * * will run at 10 minutes after and 35 minutes after.

1-59/2 * * * * 将每隔奇数分钟运行一次.

1-59/2 * * * * will run every odd minute.