OpenRC: Dependency-Based Service Management
OpenRC: Dependency-Based Service Management
Developed originally by the Gentoo Linux project and adopted by Alpine Linux, OpenRC is a dependency-based service manager that works seamlessly alongside classic init systems (such as sysvinit or BusyBox init).
+-----------------------------------------------------------+
| OpenRC Architecture |
| |
| PID 1: sysvinit or BusyBox init |
| | |
| v (invokes /sbin/openrc) |
| OpenRC Core: Computes Directed Acyclic Graph (DAG) |
| | |
| +---> Resolves `need`, `use`, `after`, `provide` |
| +---> Executes scripts in parallel or sequence |
| +---> Supervises daemons via start-stop-daemon |
| |
| Runlevels: sysinit -> boot -> default -> shutdown |
+-----------------------------------------------------------+
Why OpenRC Appeals to Distro Builders
- Lightweight & Portable: OpenRC is written in clean C99 and standard POSIX shell. It does not require
glibc, compiling cleanly onmusl-libcand BSD systems. - Transparent Shell Scripts: Unlike systemd’s monolithic binary code, OpenRC services are standard shell scripts located in
/etc/init.d/, making debugging and introspection straightforward. - Named Runlevels: Instead of arbitrary numbers, OpenRC uses human-readable runlevels:
sysinit,boot,default,nonetwork, and custom user runlevels.
Writing an OpenRC Service Script
OpenRC scripts define dependencies declaratively inside a depend() function:
#!/sbin/openrc-run
# /etc/init.d/sshd
description="OpenSSH Secure Shell Server"
command="/usr/sbin/sshd"
command_args="-D"
pidfile="/run/sshd.pid"
depend() {
need net
use logger dns
after firewall
}Dependency Semantics:
need: Mandatory dependency. The service will refuse to start if the required dependency is not available.use: Optional dependency. If present in the runlevel, it will start first; if absent, it is ignored.provide: Declares a virtual service capability. For example, bothdhcpcdandstatic-ipprovidenet. Services requiring network connectivity simply stateneed net.
Managing Services and Runlevels
# Add sshd to start automatically at boot:
# rc-update add sshd default
# Remove a service from startup:
# rc-update del apache2 default
# Start, stop, or query status:
# rc-service sshd start
# rc-service sshd status
# List all services and their assigned runlevels:
# rc-status