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:
Stage 1: Initial system setup (mounting filesystems, starting essential services).
Stage 2: Service supervision (ensuring services remain running).
Stage 3: Clean shutdown.
Why Choose Runit?
Feature | Runit | Sysvinit | Systemd |
Startup Speed | Very fast (parallelized services) | Moderate | Fast but complex |
Complexity | Simple and minimal | Simple | Complex |
Service Supervision | Native and robust | Limited | Integrated but heavy |
Dependency Handling | Basic | None | Advanced but resource-intensive |
Resource Usage | Low | Low | High |
Modularity | High | Moderate | Low |
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
Create a directory for the service:
mkdir -p /etc/sv/<service_name>
Add a
./run
script that starts the service. The service must run in the foreground. Example for agetty
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:
Create a
./log
subdirectory inside the service directory.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
Action | Command | Example |
Start a service | sv start <service_name> | sv start dhcp |
Stop a service | sv stop <service_name> | sv stop dhcp |
Restart a service | sv restart <service_name> | sv restart dhcp |
Query service status | sv status <service_name> | sv status dhcp |
Send a signal to a daemon | sv <signal> <service_name> | sv hup dhcp (to reload configuration) |
Handling Dependencies Between Services
To make one service depend on another:
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:
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
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.