php 仅在特定时间每分钟运行一次 cron 作业?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11914445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a cron job every minute only on specific hours?
提问by Jürgen Paul
How do you run a cron job every minute only on specific hours? Like so:
您如何仅在特定时间每分钟运行一次 cron 作业?像这样:
It will only check every minute from 11AM to 12AM, 4PM to 5PM and 9PM to 10PM
它只会在上午 11 点至上午 12 点、下午 4 点至下午 5 点和晚上 9 点至晚上 10 点每分钟检查一次
It seems to be complicated to me I don't know where to start.
对我来说似乎很复杂,我不知道从哪里开始。
回答by Vadim Baryshev
Right solution:
正确的解决方法:
* 11,16,21 * * *
Because if you use previous solution:
因为如果您使用以前的解决方案:
0-59 11-12,16-17,21-22 * * * *
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.
作业将在 12:40 或 17:59 开始。它不在上午 11 点至上午 12 点、下午 4 点至下午 5 点和晚上 9 点至晚上 10 点的范围内。
UPDATE:
更新:
Traditional (inherited from Unix) cron format consists of five fields separated by white spaces:
传统(继承自 Unix)cron 格式由五个由空格分隔的字段组成:
* * * * * 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)
nnCroncan use both traditional and "enhanced" version of cron format, which has an additional (6th) field: Year.
nnCron可以使用传统和“增强”版本的 cron 格式,它有一个额外的(第 6 个)字段:Year。
回答by Bharat Sinha
As per the cron format
按照cron 格式
<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>
* * * * * *
| | | | | |
| | | | | +-- Year (range: 1900-3000)
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)
the solution should be
解决方案应该是
* 11,16,21 * * * *

