You are here

protected function Provision_ShellCommand::execCommand in Aegir Objects 7.3

Run a command in a subprocess, and stream the output.

File

drush/Provision/ShellCommand.php, line 86

Class

Provision_ShellCommand

Code

protected function execCommand($command) {
  $this
    ->notice("Executing: `{$command}`");
  $descriptorspec = [
    0 => [
      'pipe',
      'r',
    ],
    // stdin
    1 => [
      'pipe',
      'w',
    ],
    // stdout
    2 => [
      'pipe',
      'w',
    ],
  ];
  $process = proc_open($command, $descriptorspec, $pipes, realpath('./'));
  if (is_resource($process)) {
    $sockets = array(
      $pipes[1],
      $pipes[2],
    );
    $write = $except = NULL;
    while (stream_select($sockets, $write, $except, $timeout = 300)) {
      foreach ($sockets as $socket) {
        if (!feof($socket) && ($output = fgets($socket))) {
          $this
            ->notice($output);
        }
        else {
          foreach ($sockets as $pipe) {
            fclose($pipe);
          }

          // Workaround for proc_close()'s propensity to return an error status.
          $status = proc_get_status($process);
          $exit = proc_close($process);
          $return = $status['running'] ? $exit : $status['exitcode'];
          if ($return === 0) {
            return $this
              ->success("Finished running: `{$command}`");
          }
          else {
            return $this
              ->error(dt('An error occured when running command! (returned :exit)', [
              ':exit' => $return,
            ]));
          }
        }
      }
    }
  }
}