<?php
while(true){
//code goes here.....
}
?>
Run Code Online (Sandbox Code Playgroud)
我想制作一个PHP Web服务器,那么如何使用Curl使这个脚本永远运行?
Tib*_*tan 23
不要忘记将最大执行时间设置为无限(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");
Run Code Online (Sandbox Code Playgroud)
如果在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);
Run Code Online (Sandbox Code Playgroud)
或者您可以使用wget从linux cron开始,如下所示:
`* * * * * wget -O - http://localhost/infinite_loop.php`
Run Code Online (Sandbox Code Playgroud)
或者您可以使用bitsadmin运行.bat文件从Windows Scheduler启动,该文件包含以下内容:
bitsadmin /create infiniteloop
bitsadmin /addfile infiniteloop http://localhost/infinite_loop.php
bitsadmin /resume infiniteloop
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9907 次 |
| 最近记录: |