A Guide to Runit: Discovering an Efficient and Minimal Init System

A Guide to Runit: Discovering an Efficient and Minimal Init System

Runit is a fast and simple init system with a primary focus on service supervision. Unlike other init systems like sysvinit or systemd, it emphasizes minimalism and reliability. Let’s dive into what makes Runit special and how to work with it.


What is Runit, and How is it Different?

Runit is an alternative init system that provides a fast and reliable way to boot up and supervise services. Unlike traditional init systems, it separates the initialization process into three clear stages:

  1. Stage 1: Initial system setup (mounting filesystems, starting essential services).

  2. Stage 2: Service supervision (ensuring services remain running).

  3. Stage 3: Clean shutdown.


Why Choose Runit?

FeatureRunitSysvinitSystemd
Startup SpeedVery fast (parallelized services)ModerateFast but complex
ComplexitySimple and minimalSimpleComplex
Service SupervisionNative and robustLimitedIntegrated but heavy
Dependency HandlingBasicNoneAdvanced but resource-intensive
Resource UsageLowLowHigh
ModularityHighModerateLow

Working with Services in Runit

Runit simplifies service management by using directories instead of traditional init scripts. Let’s go through some common tasks.

Creating a New Service Directory

  1. Create a directory for the service:

     mkdir -p /etc/sv/<service_name>
    
  2. Add a ./run script that starts the service. The service must run in the foreground. Example for a getty service:

     $ cat /etc/sv/getty-2/run
     #!/bin/sh
     exec getty 38400 tty2 linux
     $ chmod +x /etc/sv/getty-2/run
    

Adding Logging to a Service

To add logging:

  1. Create a ./log subdirectory inside the service directory.

  2. Add a ./log/run script to start a logging daemon (e.g., svlogd). Example:

     $ cat /etc/sv/socklog-klog/log/run
     #!/bin/sh
     exec chpst -ulog svlogd -tt ./main
     $ chmod +x /etc/sv/socklog-klog/log/run
    

Activating a New Service

To make Runit supervise a service:

ln -s /etc/sv/<service_name> /service/

Runit will start supervising the service within 5 seconds and ensure it starts on boot.


Managing Services

ActionCommandExample
Start a servicesv start <service_name>sv start dhcp
Stop a servicesv stop <service_name>sv stop dhcp
Restart a servicesv restart <service_name>sv restart dhcp
Query service statussv status <service_name>sv status dhcp
Send a signal to a daemonsv <signal> <service_name>sv hup dhcp (to reload configuration)

Handling Dependencies Between Services

To make one service depend on another:

  1. Modify the dependent service’s ./run script to check for the availability of the required service:

     $ cat /etc/sv/cron/run
     #!/bin/sh
     sv start socklog-unix || exit 1
     exec cron -f
    

Removing a Service

To stop and remove a service from supervision:

rm /service/<service_name>

Advanced Features of Runit

User-Specific Services

Runit supports user-specific services. To enable this:

  1. Create a service for the user:

     $ cat /etc/sv/runsvdir-floyd/run
     #!/bin/sh
     exec 2>&1
     exec chpst -ufloyd runsvdir /home/floyd/service
     $ chmod +x /etc/sv/runsvdir-floyd/run
    
  2. The user can now manage their own services in ~/service/.


Runlevels

Runit supports flexible runlevels, offering more versatility than traditional init schemes. Services for each runlevel can be defined by creating separate directories.


Compatibility with LSB Init Scripts

You don’t need to rewrite applications that rely on /etc/init.d/. Simply link the sv program:

ln -s /bin/sv /etc/init.d/<service_name>

Runit on Read-Only Filesystems

If /etc/ is mounted as read-only, you can use symbolic links to redirect writable files to a writable location (e.g., /var/run/):

ln -s /var/run/runit.stopit /etc/runit/stopit

Runit is a lightweight, reliable, and versatile init system. Whether you’re a sysadmin or a hobbyist, its simplicity and performance make it a great choice.