diff --git a/src/CLI/Console.php b/src/CLI/Console.php index f3cb3c6..921b1e8 100644 --- a/src/CLI/Console.php +++ b/src/CLI/Console.php @@ -133,20 +133,24 @@ static public function exit(int $status = 0): void */ static public function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1): int { + $cmd = '( ' . $cmd . ' ) 3>/dev/null ; echo $? >&3'; + $pipes = []; $process = \proc_open( $cmd, - [['pipe','r'],['pipe','w'],['pipe','w']], + [['pipe','r'],['pipe','w'],['pipe','w'],['pipe', 'w']], $pipes ); $start = \time(); $stdout = ''; $stderr = ''; + $status = ''; if (\is_resource($process)) { \stream_set_blocking($pipes[0], false); \stream_set_blocking($pipes[1], false); \stream_set_blocking($pipes[2], false); + \stream_set_blocking($pipes[3], false); \fwrite($pipes[0], $stdin); \fclose($pipes[0]); @@ -155,20 +159,21 @@ static public function execute(string $cmd, string $stdin, string &$stdout, stri while (\is_resource($process)) { $stdout .= \stream_get_contents($pipes[1]); $stderr .= \stream_get_contents($pipes[2]); + $status .= \stream_get_contents($pipes[3]); if ($timeout > 0 && \time() - $start > $timeout) { \proc_terminate($process, 9); return 1; } - $status = \proc_get_status($process); - - if (!$status['running']) { + if (!\proc_get_status($process)['running']) { \fclose($pipes[1]); \fclose($pipes[2]); \proc_close($process); - return (int)$status['exitcode']; + $exitCode = (int) str_replace("\n","",$status); + + return $exitCode; } \usleep(10000);