php – proc_open离开僵尸进程

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – proc_open离开僵尸进程脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
以下脚本监视/ dev / shm / test以查找新文件并实时输出有关它的信息.

问题是当用户关闭浏览器时,inotifywait进程保持打开状态,依此类推.

有什么方法可以避免这种情况吗?

<?PHP
$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","a") // stderr is a file to write to
);

$PRocess = proc_open('inotifywait -mc -e create /dev/shm/test/',$descriptorspec,$pipes);

if (is_resource($process)) {

  header("Content-type: text/htML;charset=utf-8;");
  ob_end_flush(); //ends the automatic ob started by PHP
  while ($s = fgets($pipes[1])) {
    print $s;
    flush();
  }
  fclose($pipes[1]);
  fclose($pipes[0]);
  fclose($pipes[2]);

  // It is important that you close any pipes before calling
  // proc_close in order to avoid a deadlock
  $return_value = proc_close($process);

  echo "command returned $return_value\n";
}
?>

解决方法

那是因为inotifywait将等到文件/ dev / shm / test /发生更改,然后将在标准输出输出关于标准错误和事件信息的诊断信息,并且fgets()将等到它可以读取一行:读取结束时$length – 已读取1个字节(第二个参数),或换行符(包含在返回值中)或EOF(以先到者为准).如果没有指定长度,它将继续从流中读取,直到它到达行尾.

所以基本上,您应该使用stream_set_blocking($pipes[1],0)从子进程’stdout管道非阻塞模式读取数据,或者手动检查该管道上是否存在数据为stream_select().

此外,您需要使用ignore_user_abort(true)忽略用户中止.

脚本宝典总结

以上是脚本宝典为你收集整理的php – proc_open离开僵尸进程全部内容,希望文章能够帮你解决php – proc_open离开僵尸进程所遇到的问题。

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

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