php 除了第一个小时“0”的第一分钟“0”之外,每2分钟运行一次cron作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23846952/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:00:14  来源:igfitidea点击:

Run cron job every 2 minutes except the first minute "0" of the first hour "0"

phpcron

提问by Michael Samuel

I have a simple cron job inside cPanel that runs every 2 minutes:

我在 cPanel 中有一个简单的 cron 作业,每 2 分钟运行一次:

*/2 * * * *

By default the "0" minute is the start of the cron job like 0,2,4,6,8,10 etc...

默认情况下,“0”分钟是 cron 作业的开始,如 0、2、4、6、8、10 等...

How to skip the "0" minute of the "0" hour (12AM) in the cron? I do this because this cron depends on another cron which runs once daily at 12AM. So I don't want them to overlap.

如何跳过cron中“0”小时(12AM)的“0”分钟?我这样做是因为这个 cron 依赖于另一个在每天凌晨 12 点运行一次的 cron。所以我不希望它们重叠。

In short, I need this cron to run every 2 minutes except 00:00 at 12:00AM.

简而言之,我需要这个 cron 每 2 分钟运行一次,除了 12:00 的 00:00。

回答by JakeGould

This is your cron job:

这是你的 cron 工作:

*/2 * * * *

But then you state:

但是你说:

How to skip the "0" minute of the "0" hour (12AM) in the cron? I do this because this cron depends on another cron which runs once daily at 12AM. So I don't want them to overlap.

如何跳过cron中“0”小时(12AM)的“0”分钟?我这样做是因为这个 cron 依赖于另一个在每天凌晨 12 点运行一次的 cron。所以我不希望它们重叠。

The problem is not the cron job but rather your script logic. Perhaps you could do something like create some kind of indicator in the first job that the second job would pick up on so they don't conflict.

问题不在于 cron 作业,而在于您的脚本逻辑。也许你可以做一些事情,比如在第一份工作中创建某种指标,第二份工作会接手,这样它们就不会发生冲突。

Perhaps you should just change your cron to be the inelegant, but specific of every 2 minutes like this:

也许您应该将您的 cron 更改为不优雅的,但每 2 分钟一次,如下所示:

2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * *

One concept would be to combine an interval (2-58) with a frequency (*/2), but unclear if this could work.

一个概念是将间隔 ( 2-58) 与频率 ( */2)结合起来,但不清楚这是否可行。

2-58/2 * * * *

EDIT:According to the comment left by the original poster:

编辑:根据原始海报留下的评论:

In short, I need this cron to run every 2 minutes except 12AM.

简而言之,我需要这个 cron 每 2 分钟运行一次,除了 12AM。

If that is the case, you might have to set a mix of cron jobs like this:

如果是这种情况,您可能需要像这样设置 Cron 作业的组合:

*/2 1-23 * * *
2-58/2 0 * * *

The first cron entry would run the job every 2minutes from 1:00am to 11:00pm.

第一个 cron 条目将在2凌晨 1:00 到晚上 11:00每分钟运行一次作业。

The next cron entry would run the job every 2minutes from 2-58minutes only at midnight. Since this second job skips 0during the 0hour of midnight this combo should work for you.

下一个 cron 条目将在午夜时分22-58几分钟开始每分钟运行一次作业。由于第二份工作00午夜时分跳过,因此该组合应该适合您。

But that said, this kind of dual entry logic is why it's actually encouraged that developers expand their app logic to avoid certain scenarios it won't work.

但这就是说,这种双重入口逻辑是为什么实际上鼓励开发人员扩展他们的应用程序逻辑以避免某些情况下它不起作用的原因。

For example, I have some bash scripts that create lock files in the standard Unix /tmpdirectory. They are set to run every 5 minutes, but the script itself has logic to make sure the first thing it does before anything is to check if that lock is present. If the lock is there? Do nothing. If the lock is not there, go nuts!

例如,我有一些 bash 脚本在标准 Unix/tmp目录中创建锁定文件。它们被设置为每 5 分钟运行一次,但脚本本身具有逻辑以确保它在做任何事情之前做的第一件事是检查该锁是否存在。如果锁在那里?没做什么。如果锁不在那里,那就发疯吧!