cronjob。仅在指定的时间间隔内运行php [英] cronjob. run php only in a specified interval of time

查看:79
本文介绍了cronjob。仅在指定的时间间隔内运行php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个cronjob,每35分钟执行一次php脚本操作。问题是我只希望它从第二天的上午11点运行到凌晨2点。我希望这是自动的,而不必在crontab中手动添加#。

I have set up a cronjob that excutes a php script every 35 minutes. The issue is that I only want it to run from 11 AM to 2 AM the next day. I want this to be automatic without having to add # manually to the crontab.

我尝试将类似这样的内容放在php脚本的顶部:

I've tried putting something like this at the top of the php script :

$time = date(H);

if ($time < 23 & $time > 11)
{
   echo 'Working';
}
else
{
   echo 'Stopped';
} 

但是您可以看到,仅在同一天有效。

But as you can see that only works on the same day.

注意服务器正在使用另一个时区,与我的时区有+4个小时的差异。

NOTE The server is using another time zone which has +4 hours of difference with mine.

这表示:本地时间上午11点是服务器时间下午3点,第二天本地时间凌晨3点是服务器第二天早上7点。

That means: 11 AM local time is 3 PM server time and 3 AM next day local means 7 AM next day server.

推荐答案

因此,您希望您的cronjob在一天的特定时间之间运行吗?您是否尝试过:

So you want your cronjob to run between certain hours of the day? Have you tried:

35 11-23,0-2 * * * php /path/to/your/script.php

此操作将在每天上午11点(第1天)至凌晨2点(第2天)之间每隔35分钟执行一次-一切都在服务器时间内完成。

This will execute at 35 mins past every hour between 11am (day 1) and 2am (day 2) - everything is in server time.

这篇关于cronjob。仅在指定的时间间隔内运行php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆