Question

I used pcntl extension in my code like this : I bind a handler to some signal, for example SIGUSR1, and have a script which send signals to my application.

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

And I have such an error:

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

Also I have a code for remote command execution by ssh (part of function) :

  $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);

If signal is raised here, the following error will occur: "Failure 'would block' (-9)"

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

Is there any way to avoid this?

Was it helpful?

Solution

Well, stream_get_contents function not work with unix signal normally. I use next code instead of "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; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top