Skip to content
Snippets Groups Projects
Commit 006b4454 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

exec with realtime output

parent aeb4671d
No related branches found
No related tags found
No related merge requests found
......@@ -68,12 +68,49 @@ function _exec($cmd): void
global $aCol;
echo "$aCol[blue]cmd > $cmd$aCol[reset]\n";
$iStart=microtime(true);
/*
exec("$cmd 2>&1", $aOut, $rc);
$iEnd=microtime(true);
if (!count($aOut)) {
$aOut = ["-- no output --"];
}
echo implode("\n", $aOut) . "\n";
*/
// show output in real-time
$descriptorspec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"] // stderr
];
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
// Close stdin pipe
fclose($pipes[0]);
// Read stdout and stderr in real-time
while ($output = fgets($pipes[1])) {
echo $output;
flush();
}
fclose($pipes[1]);
while ($error = fgets($pipes[2])) {
echo $error;
flush();
}
fclose($pipes[2]);
// Close the process
$rc=proc_close($process);
$iEnd=microtime(true);
} else {
_abort("Unable to execute command.");
}
$sTime= "... ⏱️ Time: " . round($iEnd-$iStart, 3) . "s\n";
if ($rc != 0) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment