如何让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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...