Last active
February 9, 2021 21:18
-
-
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
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 | |
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