Skip to content

nvs run

nvs run <file> checks a program, compiles it and runs it.

The command first checks the file the same way as nvs check. If there is an error, it prints the error and stops. The program runs with the current directory as its working directory. Before the run, nvs reads ./nvs.toml. If that file does not exist, it reads nvs.toml in the data folder, .nvsdata next to the nvs program. --config <path> reads the named file instead. Repeat the option to read several files in order.

The exit status is 0 when the program ends normally, and n when the program calls exit(n). It is 1 for a compile error, an error that nothing caught, or a limit that was reached. It is 2 for a wrong command line.

Good to know: exit("message") prints the message, and the exit status is 0. An error that nothing caught is written to standard error and never to standard output.

Examples

A check for a scheduled job

<?nvs
// Shows a check that a scheduled job runs with `nvs run 01-a-check-for-a-scheduled-job.nvs`.
// The check ends with `exit(1)` when a folder is too full. The job sees status 1 and can send an alert.
// proof: exit 1
array<string> $folders = ['uploads', 'logs', 'backups'];
array<uint> $percentFull = [42, 97, 60];
uint $problems = 0;
uint $i = 0;
foreach ($folders as string $folder) {
if ($percentFull[$i] > 90) {
echo $folder, ' is ', $percentFull[$i], "% full\n";
$problems = $problems + 1;
}
$i = $i + 1;
}
// This prints `problems: 1`. Then `exit(1)` ends the program with status 1.
if ($problems > 0) {
echo 'problems: ', $problems, "\n";
exit(1);
}
echo "all folders are fine\n";
Output
logs is 97% full
problems: 1