2017-03-01 120 views
1

我有一段脚本,可能会在一段时间后不会变为非活动状态,因为它会从会话过期时间短的非常慢的服务器请求信息。
脚本echo在完成其每个过程之后,将其作为调试代码的方式完成。是否有任何方法从另一个脚本执行脚本并在最后一次echo之后的一段时间内将其终止。换句话说,在非活动状态后杀死脚本。
编辑:这里我们可以SE我的代码示例:PHP处于非活动状态后终止进程

foreach ($info["Ten_Friends"] as $friend) { 
    $info = []; 
    echo "11111 \n"; 
    $info = $i->userAnalysis($friend); 
    if ($info != false) { 
     echo "if to database\n"; 
     if (($i->usersDatabase($info) == true)) { 
      mediaDataBase($info); 
      $nAnalized += 1; 
      echo "nAnalized: ".$nAnalized."\n"; 
     } 
    } else {echo $friend." not analised";} 

    echo "3333\n"; 
} 

正如你可以看到,每道工序后,我echo。这两个$i->userAnalysis($friend);$i->usersDatabase($info)请求数据到服务器此功能可能会在一段时间后变得没有响应,所以我需要杀死脚本,如果他们这样做。
在OSX埃尔卡皮坦和MySQL运行PHP 5.6

+0

请添加您的代码。阅读这里看看如何形成[mcve] – amflare

+0

@amflare这里是一个例子 –

回答

0

试试这个:

$cmd = "php AnalyzeFriends.php"; 
$timeout_tolerance = 10; // 10 seconds 
$last_response = time(); // start time 

ob_implicit_flush(true); 
ob_end_flush(); 

$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("pipe", "w") // stderr is a pipe that the child will write to 
); 
flush(); 
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array()); 
echo "<pre>"; 
if (is_resource($process)) { 
    while ($s = fgets($pipes[1])) { 
     print $s; 
     if((time() - $last_response) > $timeout_tolerance){ 
      fclose($pipes[0]); 
      fclose($pipes[1]); 
      fclose($pipes[2]); 
      proc_close($process); 
     } 
     $last_response = time(); 
     flush(); 
    } 
} 
echo "</pre>"; 

运行这个作为一个单独的脚本可以调用脚本您已发布,寻找回声,如果它不收到回应

+0

感谢您的答案,'$ pipe'变量会代表什么? –