Skip to content

Commit 69e68f8

Browse files
Merge pull request #15 from utopia-php/fix-exit-codes
Fix: Exit Codes
2 parents 6d164b7 + 843d5a2 commit 69e68f8

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/CLI/Console.php

Lines changed: 10 additions & 5 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+
$status = '';
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,20 +159,21 @@ 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+
$status .= \stream_get_contents($pipes[3]);
158163

159164
if ($timeout > 0 && \time() - $start > $timeout) {
160165
\proc_terminate($process, 9);
161166
return 1;
162167
}
163168

164-
$status = \proc_get_status($process);
165-
166-
if (!$status['running']) {
169+
if (!\proc_get_status($process)['running']) {
167170
\fclose($pipes[1]);
168171
\fclose($pipes[2]);
169172
\proc_close($process);
170173

171-
return (int)$status['exitcode'];
174+
$exitCode = (int) str_replace("\n","",$status);
175+
176+
return $exitCode;
172177
}
173178

174179
\usleep(10000);

0 commit comments

Comments
 (0)