runit

On this page 7

runit is a compact init scheme and service supervision suite for Unix-like systems that starts the machine in three distinct stages and keeps every configured service running under a dedicated supervisor process. In Linux system initialization it is one of the main alternatives to systemd, used as the default init on Void Linux and offered on distributions such as Artix and antiX, and it is equally common as a pure supervisor inside containers.

Written by Gerrit Pape and modeled on the daemontools design by Daniel J. Bernstein, runit favors small, auditable programs that each do one job. Services are described by plain shell scripts in directories, not by declarative unit files, and reliability comes from supervision: a crashed daemon is restarted within about a second without any external monitoring.

How runit boots a system in three stages

When runit is the init system, the kernel starts runit as process ID 1, and that program does little more than run three scripts in order. This separation keeps PID 1 tiny, which matters because a crash of PID 1 causes a kernel panic.

StageScriptResponsibilityCompletion
Stage 1/etc/runit/1One-time setup: mount filesystems, set hostname, load modulesRuns once and exits
Stage 2/etc/runit/2Starts runsvdir, which supervises all servicesRuns for the system lifetime
Stage 3/etc/runit/3Shutdown tasks: stop services, sync and unmountEnds with halt or reboot
The three runit stages

Stage 2 should never exit during normal operation. If it does, runit proceeds to stage 3 and shuts down. Shutdown and reboot are requested through the runit-init command (usually linked as init), which signals PID 1 to leave stage 2 cleanly. A failure in stage 1 drops the system into an emergency shell, giving the administrator a chance to repair it.

Service supervision with runsvdir, runsv and sv

Supervision in runit relies on a process hierarchy of small daemons. runsvdir watches a service directory, typically /var/service or /etc/service, and starts one runsv process for every subdirectory it finds; each runsv in turn runs and monitors one service.

A service is defined by a directory containing an executable run script that must start the daemon in the foreground. When the daemon exits, runsv notices immediately through the parent-child relationship and starts it again. An optional finish script runs after each exit, and a log subdirectory defines a second supervised process that receives the service’s standard output through a pipe.

ProgramRoleKey detail
runitPID 1, executes the three stagesMinimal code by design
runsvdirStarts one runsv per service directoryRescans the directory about every five seconds
runsvSupervises a single serviceRestarts it on exit, exposes control files
svCommand-line control and status toolSends commands through a control pipe
svlogdLogging daemon for service outputRotates logs by size and count
chpstChanges process state before execSets user, limits and environment
Core programs in the runit suite

Administrators interact with services through sv commands such as sv up, sv down, sv restart and sv status. Enabling a service means creating a symbolic link from its definition directory into the supervised directory; removing the link disables it.

How runit compares with systemd, OpenRC and SysVinit

runit occupies a deliberate middle ground: it offers true process supervision like systemd but keeps the small footprint and script-based configuration associated with traditional init systems. The comparison below covers the attributes that usually drive the choice.

AttributerunitsystemdOpenRCSysVinit
Service definitionShell run scriptsDeclarative unit filesShell scripts with dependenciesShell init scripts
Automatic restartBuilt in for every serviceConfigurable per unitOptional supervisorNone by default
Dependency handlingImplicit, by retryingExplicit ordering and requirementsExplicit dependenciesNumbered script order
Parallel startupYes, all services at onceYes, dependency-drivenOptionalNo
Socket activationNoYesNoNo
Loggingsvlogd per serviceCentral journalsyslogsyslog
runit and other Linux init systems

runit has no dependency graph. If a service needs another one, its run script checks the condition, for instance with sv check postgresql, and exits when it is not met; runsv then retries. This approach is simple and robust but makes startup ordering less explicit than in systemd, and features such as timers, socket activation and cgroup resource control are absent or must come from other tools.

Writing and managing runit services in practice

A good run script is short and ends by replacing itself with the daemon through exec, so that runsv supervises the daemon directly rather than an intermediate shell. Redirecting standard error to standard output with exec 2>&1 lets the logging service capture both streams.

  • Run in the foreground: pass the daemon its no-fork flag, such as -D for sshd or -g 'daemon off;' for Nginx.
  • Drop privileges with chpst, for example exec chpst -u www-data mydaemon, instead of starting as root.
  • Add a log service with exec svlogd -tt /var/log/myservice to obtain timestamped, rotated logs.
  • Use a down file in the service directory to keep a service enabled but stopped at boot.

The following decision table helps judge where runit fits best.

ScenarioFitReason
Minimal or embedded systemStrongSmall code size and fast boot
Multi-process containerStrongReliable supervision without a full init
Desktop with complex session managementWeakerMany components assume systemd interfaces
Services needing socket activation or timersWeakerRequires external tools such as cron
When runit is a suitable choice

Limitations and common mistakes with runit

Most runit problems stem from misunderstanding the foreground requirement or from expecting systemd-style features. The table lists recurring symptoms and their remedies.

SymptomLikely causeFix
Service restarts every secondDaemon forks into the backgroundUse the foreground flag
sv status shows down after linkingrun script not executableSet the executable bit
No log output appearsMissing log service or stderr redirectAdd log/run and exec 2>&1
Service starts before its databaseNo dependency checkTest with sv check and exit on failure
sv cannot find the serviceWrong SVDIR or missing symlinkLink into the active service directory
Typical runit service problems

Because runsv restarts failing services indefinitely, a misconfigured daemon can loop quietly and consume resources; checking sv status uptime values reveals services that never stay up for more than a second. Administrators moving from systemd should also expect that some software ships only unit files, so a run script must be written by hand.

Understanding it simply

When a Linux computer starts, one program must take charge of launching everything else and keeping it running. runit is a small, simple program for that job.

It works in three steps: prepare the machine, run the services, and shut down neatly. While the computer runs, runit acts like a watchful caretaker: if a service stops unexpectedly, it switches that service back on within about a second, without anyone needing to notice the problem first.

Applied in practice

A small company runs a Docker container that must host both a web application and a background job worker. Containers normally run one process, so the team installs runit inside the image and makes runsvdir /etc/service the container’s main command, with two service directories, one for the web server and one for the worker.

During a traffic spike, the worker crashes because of a memory error in a third-party library. Within a second, runsv restarts the worker automatically, and svlogd records the crash with a timestamp in its rotated log. The web application keeps answering requests throughout, and engineers later read the logs, patch the library and redeploy, having avoided an outage that would otherwise have required a manual container restart.