# Running with systemd

:::{note}
This is an alternative startup method. The supported way to run Vulnerability-Lookup is
`poetry run start`, `poetry run stop` and `poetry run update`, described in
[Installation](installation.md#usage): the website is automatically launched with the
`start` command. The units below are optional samples contributed by users. They are not
used or tested by the maintainers.
:::

## The units

Sample units are in `etc/systemd/system/`. They need systemd 249 or later (for `Upholds=`).
Each component runs as its own unit, so systemd restarts it on its own, and
`vulnerability-lookup.service` ties them together:

| Unit | Runs |
| --- | --- |
| `vulnerability-lookup.service` | Nothing itself: it starts, stops and restarts the components listed in its `Upholds=` lines, and keeps them running while it is active. `poetry run update` restarts this unit (`systemd_service_name` in `config/generic.json`). |
| `vulnerability-lookup-cache.service` | Valkey (or Redis) on `cache/cache.sock`, in the foreground. |
| `vulnerability-lookup-storage.service` | Kvrocks, in the foreground, with `storage/kvrocks.local.conf` when that file exists. |
| `vulnerability-lookup-website.service` | The website and API (gunicorn). |
| `vulnerability-lookup-feeders.service` | `feeders_manager` and the feeders it starts. Comment out its `Upholds=` line on an instance without feeders ([minimal publication](minimal-publication.md)) or with `external_crontab` set. |
| `vulnerability-lookup-index-fulltext.service` | Optional: the [full-text indexer](fulltextsearch.md), if `fulltextsearch` is `true`. |
| `vulnerability-lookup-index-attack-embeddings.service` | Optional: the ATT&CK embedding indexer, if `ATTACK_EMBEDDING_INDEXER` and `ML_GATEWAY` are set in `config/website.py`. |

The [watchlist notification](notification.md) and [sync](sync.md) services have their own
units, on their pages. They require `vulnerability-lookup.service`, so they work with these
units unchanged. To have `vulnerability-lookup.service` keep them running too, uncomment their
`Upholds=` lines.

The two backends count as started only once they answer, and the other units wait for them.
The website, the feeders and the indexers are bound to both backends: when the cache or the
storage stops or crashes, they stop with it, and start again once it is back. Without that,
the feeders would not come back after the cache crashes: a feeder that loses the cache exits
with status 0, which `feeders_manager` does not respawn.

systemd watches `feeders_manager`, not each feeder, and the unit stays active when
`feeders_manager` leaves one down: a feeder that exits with status 0 for another reason, or
one that fails three times in a row within a minute of starting, as on a configuration error.
Its journal says which: `journalctl -u vulnerability-lookup-feeders | grep 'not respawning'`.

`vulnerability-lookup-feeders.service` caps the feeders at half of the host's memory
(`MemoryMax=50%`): a feeder that goes over it is killed inside the unit, and `feeders_manager`
respawns it. Adjust it to the host.

## Environment

A unit gets none of the environment of the shell you started the components from before.
The website, the feeders and the other Python units read `/etc/environment`, where a proxy
for the whole host (`http_proxy`, `https_proxy`, `no_proxy`) is usually set. As with
`poetry run start`, they also read the `.env` file created during
[installation](installation.md#dependencies), one `KEY=value` per line: put there what
`config/website.py` reads from the environment, such as a pinned `OIDC_CLIENT_SECRET`
([single sign-on](sso.md)), and keep the file readable by its owner only (`chmod 600 .env`).
The units do not set `VULNERABILITYLOOKUP_HOME` themselves: the components read `.env` only
while it is unset.

## Installing

If the instance runs under the unit described in
[Installation](installation.md#launching-the-website-with-systemd), stop it first: it runs
`poetry run start`, which would start a second copy of every component. Stopping it runs
`poetry run stop`. That unit is named `vulnerability-lookup.service`, like the sample that
replaces it:

```bash
sudo systemctl stop vulnerability-lookup.service
```

Older versions of that page named it `vulnerability-lookup-web.service`. Stop and remove it:

```bash
sudo systemctl disable --now vulnerability-lookup-web.service
sudo rm /etc/systemd/system/vulnerability-lookup-web.service
```

If you started the instance with `poetry run start` instead, stop it with `poetry run stop`.

The notification and sync units stop with it, as they require it. If theirs still require
`vulnerability-lookup-web.service`, change it to `vulnerability-lookup.service` in their
`After=` and `Requires=` lines, and uncomment their `Upholds=` lines below so that
`vulnerability-lookup.service` starts them again.

Copy the samples to `/etc/systemd/system/` without the `.sample` suffix, replace every
`<...>` placeholder, and in `vulnerability-lookup.service`, uncomment the `Upholds=` lines of
the optional components you installed. Then:

```bash
sudo systemctl daemon-reload
sudo systemctl enable vulnerability-lookup.service
sudo systemctl start vulnerability-lookup.service
```

To add a component later, install its unit, uncomment its `Upholds=` line, and run
`sudo systemctl daemon-reload`: `vulnerability-lookup.service` starts it.

## Day to day

Use `systemctl status 'vulnerability-lookup*'`, `journalctl -u 'vulnerability-lookup*'` and
`sudo systemctl restart vulnerability-lookup`. A single component can be restarted on its
own, for example `sudo systemctl restart vulnerability-lookup-feeders` after a change to
`config/modules.cfg`.

While `vulnerability-lookup.service` is active, a component stopped on its own is started
again at once. To keep one stopped, release them all first (this stops none of them), and
start `vulnerability-lookup.service` again when done:

```bash
sudo systemctl stop --job-mode=ignore-dependencies vulnerability-lookup.service
sudo systemctl stop vulnerability-lookup-feeders.service
# ...
sudo systemctl start vulnerability-lookup.service
```

Meanwhile `poetry run update` does not find `vulnerability-lookup.service` active, and
restarts the instance with `poetry run stop` and `poetry run start`, outside systemd: start
`vulnerability-lookup.service` again before updating.

A stop or a restart asks the feeders to stop, as `poetry run stop` does: an idle feeder stops
within 10 seconds, and one in the middle of a run gets up to a minute to finish it. Whatever
still runs then is stopped, and does its run again at the next start. The indexers likewise get
a minute to send what they hold. `systemctl stop vulnerability-lookup` returns once every
component has stopped.

:::{note}
Do not use `poetry run start` or `poetry run stop` on a host managed this way: `start` launches
a second copy of every component outside systemd, and the backends that `stop` shuts down are
started again.

`poetry run update` restarts the services with `sudo service vulnerability-lookup restart`. If
the user who runs it cannot use sudo, that last step fails: run
`sudo systemctl restart vulnerability-lookup` from an account that can.
:::
