仅在特定时间每分钟运行一次 cron 作业?

2022-01-03 00:00:00 cron centos php

您如何仅在特定时间每分钟运行一次 cron 作业?像这样:

How do you run a cron job every minute only on specific hours? Like so:

它只会在上午 11 点到中午 12 点、下午 4 点到下午 5 点和晚上 9 点到晚上 10 点每分钟检查一次

It will only check every minute from 11AM to 12AM, 4PM to 5PM and 9PM to 10PM

这对我来说似乎很复杂,我不知道从哪里开始.

It seems to be complicated to me I don't know where to start.

推荐答案

正确的解决方案:

* 11,16,21 * * *

因为如果您使用以前的解决方案:

Because if you use previous solution:

0-59 11-12,16-17,21-22 * * * *

作业将在 12:40 或 17:59 开始.不在上午 11 点到 12 点、下午 4 点到 5 点和晚上 9 点到 10 点的范围内.

Job will start at 12:40 or 17:59. It is not in range from 11AM to 12AM, 4PM to 5PM and 9PM to 10PM.

更新:

传统(继承自 Unix)cron 格式由五个由空格分隔的字段组成:

Traditional (inherited from Unix) cron format consists of five fields separated by white spaces:

*    *    *    *    *  command to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 6) (0 is Sunday, or use names)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

nnCron 可以使用传统和增强"版本的 cron 格式,它有一个额外的(第 6 个)字段:年份.

nnCron can use both traditional and "enhanced" version of cron format, which has an additional (6th) field: Year.

相关文章