如何让PHP脚本永远与Cron Job一起运行?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了如何让PHP脚本永远与Cron Job一起运行?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<?PHP
 while(true){
 //code goes here.....
 }
  ?>

我想制作一个PHP Web服务器,那么如何使用Curl使这个脚本永远运行?

不要忘记将最大执行时间设置为无限(0).

如果这是你的意图,那么最好确保你不要运行多个实例:

ignore_user_abort(true);//if caller closes the connection (if inITiating with cURL From another PHP,this allows you to end the calling PHP script without ending this one)
set_time_limit(0);

$hLock=foPEn(__FILE__.".lock","w+");
if(!flock($hLock,LOCK_EX | LOCK_NB))
    die("Already running. exiting...");

while(true)
{

    //avoid cpu exhaustion,adjust as necessary
    usleep(2000);//0.002 seconds
}

flock($hLock,LOCK_UN);
fclose($hLock);
unlink(__FILE__.".lock");

如果在CLI模式下,只需运行该文件.

如果在Web服务器上的另一个PHP中,您可以启动必须像这样运行的那个(而不是使用cURL,这消除了依赖):

$cx=stream_context_create(
    array(
        "http"=>array(
            "timeout" => 1,//at least PHP 5.2.1
            "ignore_errors" => true
        )
    )
);
@file_get_contents("http://localhost/infinite_loop.PHP",false,$cx);

或者你可以使用wget从linux cron开始,如下所示:

`* * * * * wget -O - http://localhost/infinite_loop.PHP`

或者您可以使用bitsadmin运行.bat文件从Windows Scheduler启动,该文件包含以下内容

bitsadmin /create infiniteloop
bitsadmin /adDFile infiniteloop http://localhost/infinite_loop.PHP
bitsadmin /resume infiniteloop

脚本宝典总结

以上是脚本宝典为你收集整理的如何让PHP脚本永远与Cron Job一起运行?全部内容,希望文章能够帮你解决如何让PHP脚本永远与Cron Job一起运行?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。