Skip to content

Commit 4ea9996

Browse files
committed
Improve exit code logic
1 parent 2dd8e1a commit 4ea9996

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/CLI/Console.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,24 @@ static public function exit(int $status = 0): void
133133
*/
134134
static public function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1): int
135135
{
136+
$cmd = '( ' . $cmd . ' ) 3>/dev/null ; echo $? >&3';
137+
136138
$pipes = [];
137139
$process = \proc_open(
138140
$cmd,
139-
[['pipe','r'],['pipe','w'],['pipe','w']],
141+
[['pipe','r'],['pipe','w'],['pipe','w'],['pipe', 'w']],
140142
$pipes
141143
);
142144
$start = \time();
143145
$stdout = '';
144146
$stderr = '';
147+
$statusPipe = '';
145148

146149
if (\is_resource($process)) {
147150
\stream_set_blocking($pipes[0], false);
148151
\stream_set_blocking($pipes[1], false);
149152
\stream_set_blocking($pipes[2], false);
153+
\stream_set_blocking($pipes[3], false);
150154

151155
\fwrite($pipes[0], $stdin);
152156
\fclose($pipes[0]);
@@ -155,6 +159,7 @@ static public function execute(string $cmd, string $stdin, string &$stdout, stri
155159
while (\is_resource($process)) {
156160
$stdout .= \stream_get_contents($pipes[1]);
157161
$stderr .= \stream_get_contents($pipes[2]);
162+
$statusPipe .= \stream_get_contents($pipes[3]);
158163

159164
if ($timeout > 0 && \time() - $start > $timeout) {
160165
\proc_terminate($process, 9);
@@ -166,7 +171,9 @@ static public function execute(string $cmd, string $stdin, string &$stdout, stri
166171
if (!$status['running']) {
167172
\fclose($pipes[1]);
168173
\fclose($pipes[2]);
169-
$exitCode = \proc_close($process);
174+
\proc_close($process);
175+
176+
$exitCode = (int) str_replace("\n","",$statusPipe);
170177

171178
return $exitCode;
172179
}

0 commit comments

Comments
 (0)