0

我在我的代码中使用了 pcntl 扩展,如下所示:我将处理程序绑定到某个信号,例如 SIGUSR1,并有一个向我的应用程序发送信号的脚本。

pcntl_signal(SIGUSR1, function ($signo){
 echo 'Signal:' . $signo . PHP_EOL; 
});

我有这样一个错误:

stream_get_contents(): Failure 'would block' (-9) 

我还有一个通过 ssh 执行远程命令的代码(功能的一部分):

  $stream = ssh2_exec(
    $this->connection,
    $command,
    $options['pty'],
    $options['env'],
    $options['width'],
    $options['height'],
    $options['width_height_type']
  );
  if ($options['waitOut']) {
    stream_set_blocking($stream, true);

如果在此处引发信号,则会出现以下错误:“Failure 'would block' (-9)”

    $output = stream_get_contents($stream);
  }
  fclose($stream);
  return $output; 

有没有办法避免这种情况?

4

1 回答 1

0

好吧,stream_get_contents 函数不能正常使用 unix 信号。我使用下一个代码而不是“stream_get_contents”

  $finisher = "SSH_FINISHED_COMMAND";
  $command .= " && echo \"{$finisher}\"";
  $command  = str_replace(' &&' , ' 2>&1 &&', $command);
  $stream = ssh2_exec(
      $this->connection,
      $command,
      $options['pty'],
      $options['env'],
      $options['width'],
      $options['height'],
      $options['width_height_type']
  );
  stream_set_blocking($stream, true);
  $block = false;
  $isFinished = false;
  while (! feof($stream) && ! $isFinished){
       $block = fread($stream, 256);
       $output .= $block;
       $isFinished = substr($output , -strlen($finisher)-2) == $finisher . '\n';           
  }
  $output = str_replace($finisher . "\n", '', $output);
  fclose($stream);
  return $output; 
于 2013-04-04T10:47:01.100 回答