加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – proc_open离开僵尸进程

发布时间:2020-12-13 22:50:04 所属栏目:PHP教程 来源:网络整理
导读:以下脚本监视/ dev / shm / test以查找新文件并实时输出有关它的信息. 问题是当用户关闭浏览器时,inotifywait进程保持打开状态,依此类推. 有什么方法可以避免这种情况吗? ?php$descriptorspec = array( 0 = array("pipe","r"),// stdin is a pipe that the
以下脚本监视/ 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_valuen";
}
?>

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读