Last active
August 13, 2021 15:24
-
-
Save Pamblam/708c5cfdd747871185a762f4ad0de613 to your computer and use it in GitHub Desktop.
run a command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Run a command with optional stdin | |
* @param string $cmd - the command to run | |
* @param string|string[] $stdin - the stdin to feed to the command | |
* @param string $cwd - the working directory in which to run the command | |
* @return object containing exit_status, stdout, stderr and elapsed | |
*/ | |
function runcmd($cmd, $stdin=null, $cwd=null){ | |
if(empty($cwd)) $cwd = getcwd(); | |
if(empty($stdin)) $stdin = array(); | |
if(!is_array($stdin)) $stdin = array($stdin); | |
$started = microtime(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 | |
); | |
$process = proc_open($cmd, $descriptorspec, $pipes, $cwd); | |
$stderr = ''; | |
$stdout = ''; | |
$status = 0; | |
if (is_resource($process)) { | |
foreach($stdin as $input){ | |
fwrite($pipes[0], $input); | |
} | |
fclose($pipes[0]); | |
$stdout = stream_get_contents($pipes[1]); | |
fclose($pipes[1]); | |
$stderr = stream_get_contents($pipes[2]); | |
fclose($pipes[2]); | |
$status = proc_close($process); | |
}else{ | |
$stderr = 'Unable to run command.'; | |
$status = 1; | |
} | |
$elapsed = microtime(true) - $started; | |
return (object) array( | |
'exit_status' => $status, | |
'stderr' => $stderr, | |
'stdout' => $stdout, | |
'elapsed' => $elapsed | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment