nvs service
nvs service installs a server as a service of the operating system, which starts it at every boot.
nvs service install stores one nvs command under a name you choose. You write the command after --, and it must be nvs serve or nvs run. Every path in it must be a full path. start, stop and status control the service. unit prints what install stores. uninstall removes the service.
The service manager is systemd on Linux and the Service Control Manager on Windows. You install a service as root on Linux, and as an administrator on Windows.
install and status check the owner of each --config file for the service's account. If that account would not accept a file, install stops with E0607 and changes nothing.
A port from 1 to 1023 is a privileged port. For a server on one, install on Linux also writes a systemd socket unit. systemd listens for the server, so the service needs no extra permission.
Good to know: a service has no terminal. Set [log] target in nvs.toml to the file for the server's log.
The examples below read the log file that a service uses, and show which ports are privileged.
Examples
The examples below run with this configuration file.
# The configuration of a shop server that runs as a service.# A service has no terminal, so the log of the server is a file.[log]target = "file:/srv/shop/logs/novis.log"The settings a service reads
<?nvs// Shows the setting in the `nvs.toml` beside this file that a server needs when it runs as a service.
// A service has no terminal. `[log] target` is the file that gets the log of the server.?string $log = Core\Config::get('log.target');echo 'log target: ', $log ?? 'not set', "\n";
// A program can read this setting, but it cannot change it, so `Core\Config::set('log.target', ...)`// does not compile. Here the name is in a variable, so the call runs and returns `false`.string $name = 'log.target';echo 'changed by the program: ', Core\Config::set($name, 'file:/tmp/other.log') ? 'yes' : 'no', "\n";log target: file:/srv/shop/logs/novis.logchanged by the program: noA service on port 443
<?nvs// Shows which ports make `nvs service install` write a socket unit beside the service unit.// A port from 1 to 1023 is a privileged port. Only root may listen on one.
// These are the ports of `listen = ["0.0.0.0:443", "127.0.0.1:8000"]` in `[server]`.array<int> $ports = [443, 8000];
// Port 443 is privileged, so `install` also writes `shop.socket`.// systemd then listens on both addresses, and the service runs without root.foreach ($ports as int $port) { echo 'port ', $port, ': ', $port < 1024 ? 'privileged' : 'not privileged', "\n";}port 443: privilegedport 8000: not privileged