| /| /
|/ |/iki Map About | Page New Edit History
# Runit
## Usage
- sv status, sv s
> prints status of service
- sv up, sv u
> start service, restart if stops
- sv down, sv d
> stop service, do not restart
- sv once, sv o
> start service, do not restart
- sv pause, cont, hup, quit, term, kill
> restarts service
- sv exit, sv e
> kill service, do not restart
## User services
Create a service named `runsvdir-` with `run` script containing:
```bash
#!/bin/sh
export USER=""
export HOME="/home/"
groups="$(id -Gn "$USER" | tr ' ' ':')"
svdir="$HOME/"
exec chpst -u "$USER:$groups" runsvdir "$svdir"
```
Afterwards you can export `SVDIR="$HOME/"` for convenience.
## Service management
### Service structure
```
*run - (mandatory) starts process
*check - exit 0 means service is running
*finish - is ran after any shutdown (for cleanup)
control/
├── *c - replaces SIGCONT
├── *d - is ran after control/t for down
├── *x - is ran after control/t for exit
├── *t - replaces SIGTERM
└── *u - is ran before run
log/ - managed by sv, contains logs
supervise/ - managed by sv
```
### Files
#### run
Mandatory (everything else is optional) script that is ran to start the service.
#### check
Script that performs manual check of service status and returns `0` if service is up and running or any non-zero code otherwise.
#### finish
A cleanup script. Is ran last after service is terminated by any means, including crashes.
#### control/d and control/x
Ran after `SIG____` when `sv` is called with either `down` or `exit` respectively.
#### control/t, control/c, control/h, etc
Scripts that replace `SIGTERM`, `SIGCONT` and `SIGHUP` respectively. Can be used to control the way that service is shut down. If they fail to stop service then they must exit with any non-zero code.
The letter comes from first letter that comes after `SIG`.
#### control/u
Ran when `sv` is called with `up` before the `run` script.
### Using with screen
#### Run
```bash
#!/bin/sh
# exec seems to be crucial
exec screen -DmS session_name program
```
#### Stop, term and down
```bash
#!/bin/sh
screen -S session_name -X kill
# or if needs to be terminated with command
screen -S session_name -X stuff stop
sleep 5
```
### Do not autostart
Create `/path/to/service/down`
### Fast check status as exit code
```bash
sv status devdocs | perl -ne 'exit(/^run:/gi ? 0 : 1)' && $() || $()
```
________________________________________________________________________________
Alisa Lain (C) 2025-2026