如何设置CRON作业以使其从9:24:59到15:14:59每10分钟准确运行一次?

时间:2019-07-22 22:00:41

标签: php cron cpanel cron-task

我需要以非常精确的时间为我的PHP代码设置2个cron作业。

Cron 1:

每10分钟执行一次定时任务,每天9:24:59至15:14:59。 即Cron工作从每天早上9小时24分59秒开始到下午15小时14分59秒开始。 cron应该每10分钟运行一次, 示例:

9:24:59
9:34:59
9:44:59
9:54:59
...
14:54:59
15:04:59
15:14:59

Cron 2:

每天9:15到15:05之间每10分钟执行一次cron作业。 例如:

9:15
9:25
9:35
9:45
...
14:45
14:55
15:05

2 个答案:

答案 0 :(得分:0)

要使其每10分钟运行一次任何进程,只需添加:

* / 10 * * * *

我会通过PHP函数来限制时间。

Crontabs explanation

Source

答案 1 :(得分:0)

您可以使用crontab文件语法*/10 * * * *将cron作业配置为每10分钟运行一次。要在 9:24:59 15:14:59 之间运行cron作业,可以将以下检查添加到Php脚本中:

$time1        = strtotime(date("d-m-Y") . " 09:24:59");
$time2        = strtotime(date("d-m-Y") . " 15:14:59");
$current_time = time();

if ($current_time < $time1 && $current_time > $time2)
    die("Script is not scheduled to run");

上面的代码检查当前时间是否在给定的时间范围内。如果不是,则脚本结束

还可以在cron选项卡命令中执行时间范围检查。例如,您可以创建一个bash脚本来检查当前时间是否在给定范围内。如果不是,则脚本结束,否则可以运行cron作业。

有关如何检查bash中的时间范围,请参见How to check if the current time is between 23:00 and 06:30。另请参见Cron jobs and random times, within given hours。它说明了在运行cron作业之前如何运行脚本