Skip to content

Core\Process\Result::exitCode

$result->exitCode(): int

Returns the exit code of a program that has ended. A program sets this number when it ends, to say whether its work succeeded. By convention, 0 means success and any other number means failure. Many tools give each kind of failure its own number, and their manual lists them.

On Linux and macOS, a program can also be stopped by a signal before it sets an exit code, for example after kill. The exit code is then -1. On Windows, a stopped program has an exit code that is not 0.

The program has already ended when you have a Core\Process\Result, so exitCode never throws an error. It returns the same number every time you call it.

The examples below tell success from failure, read the exit code of a program that was stopped, and run a list of checks before a job starts.

Return value

Returns int. The exit code, as an int. On Linux and macOS, it is -1 when a signal stopped the program before it could return an exit code.

Examples

Success or failure

<?nvs
// Shows how to tell whether a program succeeded from its exit code.
// requires: unix
// `true` does nothing and succeeds. This prints `0`.
echo Core\Process::run('/usr/bin/true', [])->exitCode(), "\n";
// `false` does nothing and fails. This prints `1`.
echo Core\Process::run('/usr/bin/false', [])->exitCode(), "\n";
// A shell script can choose its own exit code. This prints `42`.
echo Core\Process::run('/bin/sh', ['-c', 'exit 42'])->exitCode(), "\n";
Output
0
1
42

A program that was stopped

<?nvs
// Shows the exit code of a program that was stopped before it could end by itself.
// requires: unix
// `sleep 60` waits for one minute. `kill()` stops it at once.
var $program = Core\Process::spawn('/bin/sleep', ['60']);
$program->kill();
// A stopped program did not set an exit code. On Linux and macOS, the result is `-1`.
var $result = $program->wait();
echo 'exit code: ', $result->exitCode(), "\n";
// `exitCode()` returns the same number on every call. This prints `-1` again.
echo 'again: ', $result->exitCode(), "\n";
Output
exit code: -1
again: -1

Checks before a job starts

<?nvs
// Shows a job that runs a list of checks first, and stops if one of them fails.
// requires: unix
// Each check is a shell test. `test` ends with `0` when the test is true.
array<string> $checks = [
'the examples folder exists' => 'test -d docs/examples',
'the settings file exists' => 'test -f nvs.toml',
'the backup folder exists' => 'test -d /no-such-backup-folder',
];
int $failed = 0;
foreach ($checks as string $name => string $check) {
int $code = Core\Process::run('/bin/sh', ['-c', $check])->exitCode();
if ($code == 0) {
echo 'ok: ', $name, "\n";
} else {
echo 'failed: ', $name, ' (exit code ', $code, ")\n";
$failed = $failed + 1;
}
}
// One check failed, so this prints that the job does not start.
if ($failed > 0) {
echo 'The job does not start: ', $failed, " check failed.\n";
}
Output
ok: the examples folder exists
ok: the settings file exists
failed: the backup folder exists (exit code 1)
The job does not start: 1 check failed.