Skip to content

Core\Script\ExitReport::status

$exitReport->status(): int

Core\Script\ExitReport::status() returns the exit status of the process, as a whole number. A status of 0 means success. After a normal end and after Core\Script::finish(), the status is 0. After exit($n), the status is $n. After an error that nothing caught, the status is 1.

You read the report inside a function that you added with Core\Script::onExit(). The status is fixed before the first function runs, and a function cannot change it.

The examples below show the status after the last line, the status after an error that nothing caught, and a job that writes its status in a log line for a monitoring tool.

Return value

Returns int. 0 for a normal end and after Core\Script::finish(). After exit($n), it is $n. After an exception that nothing caught, it is 1. The status is fixed before the first function runs.

Examples

The last line ran

<?nvs
// Shows the exit status after the last line of the script runs.
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
// This prints "status 0".
echo 'status ', $report->status(), "\n";
});
echo "the report is ready\n";
Output
the report is ready
status 0

An error that nothing catches

<?nvs
// Shows the exit status after an error that nothing catches.
// proof: exit 1
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
// This prints "status 1".
echo 'status ', $report->status(), "\n";
});
echo "reading the price list\n";
throw new RuntimeError('the price list is empty');
Output
reading the price list
status 1

A log line for a monitoring tool

<?nvs
// Shows a backup job that writes its exit status in a log line. A monitoring
// tool reads the line and shows the job as green or red.
// proof: exit 2
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => {
string $state = $report->status() == 0 ? 'green' : 'red';
// This prints "job=backup status=2 state=red".
echo 'job=backup status=', $report->status(), ' state=', $state, "\n";
});
int $failed = 2;
echo 'copied 40 files, ', $failed, " failed\n";
if ($failed > 0) {
// The status 2 means "some files were not copied".
exit(2);
}
Output
copied 40 files, 2 failed
job=backup status=2 state=red