Skip to content

Instantly share code, notes, and snippets.

@n8jadams
Last active February 9, 2021 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8jadams/41fccb77b1481cfebf52380b2213bc6a to your computer and use it in GitHub Desktop.
Save n8jadams/41fccb77b1481cfebf52380b2213bc6a to your computer and use it in GitHub Desktop.
Execute a command and pipe its output to stdout (echo it) as it's recieved, rather than wait for the command to finish executing
<?php
function execAndPipeOutput($cmd)
{
echo PHP_EOL;
$proc = proc_open($cmd, [['pipe','r'],['pipe','w'],['pipe','w']], $pipes);
while(($line = fgets($pipes[1])) !== false) {
fwrite(STDOUT, $line);
}
while(($line = fgets($pipes[2])) !== false) {
fwrite(STDERR, $line);
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($proc);
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment