Skip to content

Instantly share code, notes, and snippets.

@jippi
Created June 7, 2012 06:01
Show Gist options
  • Select an option

  • Save jippi/2886817 to your computer and use it in GitHub Desktop.

Select an option

Save jippi/2886817 to your computer and use it in GitHub Desktop.
<?php
namespace nodes;
use \nodes\Log;
class Command {
public static function execute($command, $cwd = null, $env = null, $allowSudo = true) {
$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", "w") // stderr is a pipe that the child will write to
);
Log::debug("Executing command: $command");
if (!empty($cwd)) {
Log::debug("--> cwd = $cwd");
}
// Execute command
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
if (!is_resource($process)) {
Log::err("Could not execute command: $command");
throw new Exception("Could not execute command: $command");
}
// close stdin
fclose($pipes[0]);
$stdout = trim(stream_get_contents($pipes[1]));
fclose($pipes[1]);
Log::debug("--> stdout:");
Log::debug($stdout);
$stderr = trim(stream_get_contents($pipes[2]));
fclose($pipes[2]);
if (!empty($stderr)) {
Log::out("--> stderr:");
Log::out($stderr);
} else {
Log::debug('--> stderr:');
Log::debug($stderr);
}
$exit_code = proc_close($process);
Log::debug("--> exit_code: $exit_code");
unset($pipes[0], $pipes[1], $pipes[2], $pipes);
unset($descriptorspec[0], $descriptorspec[1], $descriptorspec[2], $descriptorspec);
return compact('stdout', 'stderr', 'exit_code', 'command', 'cwd', 'env');
}
}
<?php
$info = Command::execute('/var/www/manager/htdocs/bin/sftp-transfer-file ' . $servers . ' --version "' . $this->Configuration->getVersion() . '" --project "' . $this->Configuration->get('Project.name') . '"');
if ($info['exit_code'] != 0) {
Log::err($info['stderr']);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment