PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何让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启动,该文件包含以下@L_696_15@:

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

大佬总结

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

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

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