Created
January 31, 2024 12:13
-
-
Save diloabininyeri/2355b3a4123a847826ee7be8027ba4b7 to your computer and use it in GitHub Desktop.
Get a live output of any command in the php ...
This file contains 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 | |
//Dilo surucu | |
$spec = array( | |
0 => array('pipe', 'r'), // stdin | |
1 => array('pipe', 'w'), // stdout | |
2 => array('pipe', 'w') // stderr | |
); | |
$sudoPass = 'sudo password'; | |
$process = proc_open("echo $sudoPass| sudo -S docker-compose up -d --build", $spec, $pipes); | |
if (is_resource($process)) { | |
[, $stdout, $stderr] = $pipes; | |
stream_set_blocking($stdout, 0); | |
stream_set_blocking($stderr, 0); | |
while (true) { | |
$read = array($stdout, $stderr); | |
$write = null; | |
$except = null; | |
if (stream_select($read, $write, $except, 0, 50000)) { | |
foreach ($read as $stream) { | |
$outputLine = fgets($stream); | |
if ($outputLine !== false) { | |
echo $outputLine; | |
flush(); | |
ob_flush(); | |
echo '<br>'; | |
} | |
} | |
} | |
$status = proc_get_status($process); | |
if (!$status['running']) { | |
break; | |
} | |
usleep(50000); | |
} | |
fclose($pipes[0]); | |
fclose($pipes[1]); | |
fclose($pipes[2]); | |
$status = proc_get_status($process); | |
proc_close($process); | |
echo "Done.\n"; | |
} else { | |
echo "The command can't start.\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment