Consider the following pseudo code:
$SOME_PROCESS = proc_open(/* something here */);
...
$status = proc_get_status($SOME_PROCESS);
...
$exitCode = proc_close($SOME_PROCESS);
If the external program has exited on its own before the call to proc_get_status, then $exitCode == -1
So consider using:
$actualExitCode = ($status["running"] ? $exitCode : $status["exitcode"] );