Run as a service
nvs service installs a server as a service of the operating system. The service starts at every
boot, and it starts again after a failure. On Linux the service manager is systemd. On Windows it
is the Service Control Manager.
Install the service
Section titled “Install the service”Give the service a name. Everything after -- is the command that the service runs:
nvs service install shop -- serve /srv/shop/public/index.nvsThe command asks for admin rights itself. On Windows it shows the prompt for admin rights, and on
Linux and macOS it runs sudo. Admin rights says what happens
where no prompt can show.
The installer checks the command before it changes anything:
- Every path must be a full path, also the folder after
--data. A service does not start in the folder of your terminal. - The command must be
serveorrun. - The command must not contain a password. Other users of the machine can read a command line.
The configuration file
Section titled “The configuration file”--config is optional. Without it, the service reads nvs.toml in its data folder. The data
folder is .nvsdata next to the nvs program, or the folder that --data names after --. The
installer creates the folder and the file when they do not exist. In the new file, every setting
is present and commented out. A file that already exists is not changed.
If the installer cannot create or use the data folder, it stops with the error E0653 and installs
nothing. This happens only when the service reads its nvs.toml from the data folder.
To use another configuration file, name it with a full path:
nvs service install shop -- serve /srv/shop/public/index.nvs --config /srv/shop/nvs.tomlThe installer checks the configuration
Section titled “The installer checks the configuration”A server checks who owns its configuration file, and it does not start when the check fails. The
installer checks every --config file in the same way before it installs the service. It checks
for the account that the service runs as. The
account is the one that --account names. Without --account, it is LocalSystem on Windows and
root on Linux.
A file passes when its owner is that account or an administrator, and no group of ordinary
accounts can write to it. If a file fails, the installer stops with the error E0607 and changes
nothing. The help: line under the error prints the command that fixes it. --dry-run runs the
same check, prints the same error, and the exit status is 1.
A --config file that does not exist yet passes, because the installer writes it. On Windows,
the installer runs as administrator, so Administrators owns the data folder it creates. Every
service account accepts that folder.
To run the same check without the installer, use
nvs config check --as <account>.
What the service account can change
Section titled “What the service account can change”With --account, the service runs as that account. The account can read the data folder and
every configuration file. It can write only to these folders in the data folder:
cache, the compiled programstmp, the folders thatCore\IO::createTemporaryDirectorycreateslsp, files for the editorlogs, the log filesdebug, the requests that[debug] storerecords fornvs debug
It can also write to a log, cache or temporary folder that your configuration names. The service cannot change its own configuration.
On Linux, the account must exist before you install the service. You can create one with
useradd --system --no-create-home shop. The installer gives the data folder and its nvs.toml to
root, and the group of the account can read them. The folders that the service writes to belong to
the account, and only the account can open them.
If your --config file is in another folder, the
installer does not change it. It prints a warning when the account cannot read that file.
nvs service uninstall makes all of these folders private to root again.
The log
Section titled “The log”A service has no terminal. When nvs.toml does not set [log] target, the service writes its log
to logs/nvs.log in the data folder.
When the file reaches 10 MB, Novis renames it to nvs.log.1 and starts a new file. It keeps 5
old files and deletes older ones. So the log never uses more than 60 MB. Two settings change this:
[log]max_size = "50M" # the size at which the file is renamedkeep = 10 # how many old files are kept[log] target writes the log to another file instead. The same two settings apply to it.
Some errors happen before the server has read its configuration, for example a compile error. These go to the standard error stream. On Linux, systemd keeps it in the journal. On Windows, it goes to the Event Log. When the server cannot use its data folder, it writes its whole log to the standard error stream too.
See what it installs
Section titled “See what it installs”nvs service unit prints what install would write, and changes nothing. On Linux it prints a
systemd unit. On Windows it prints a New-Service command. You can read it, or install it with
your own tools.
Start, stop and check
Section titled “Start, stop and check”nvs service start shopnvs service status shopnvs service stop shopA stop finishes the requests that are running, and then the server exits. status prints the state
of the service.
When the service is not running, status also prints the end of its last run, under Last run:.
On Windows, these are the last 20 warnings and errors in the event log. On Linux, these are the last
20 lines in the journal. If an error stopped the service, you see the error here.
Then status checks every --config file for the account the service runs as. This is the same
check that the installer runs. A file that the account would not accept prints the error E0607,
and the exit status is 1. A file that your terminal cannot read prints a warning, and the exit
status stays 0. A file in a private folder is an example. Run status in an administrator
terminal or with sudo to check it.
nvs service run shop runs the same command in your terminal, so you can read the error when the
service does not start.
A new version of your application needs no restart. Run nvs reload as the service account or as
an administrator, and the server uses the new files at once.
Deploy a new version shows the whole deploy.
Useful options
Section titled “Useful options”| Option | What it sets |
|---|---|
--account <account> |
the account that runs the service |
--start automatic, delayed or manual |
when the service starts after a boot |
--depends-on <service> |
a service that must start first, such as a database |
--dry-run |
prints every step and changes nothing |
Do and don’t
Section titled “Do and don’t”- Do run the service as an account of its own, with
--account. - Do read
logs/nvs.login the data folder when something goes wrong. - Do try
nvs service runwhen the service does not start. - Don’t put a password on the command line. Use
password_fileinnvs.toml.
Read every option in Reference: nvs service.
