A Brief Introduction to OpenRC
2023-04-22
OpenRC is a process management system designed for Unix-like systems, automatically managing background services during system startup and shutdown.
OpenRC is developed and maintained by the Gentoo community. In addition to Gentoo Linux, Alpine Linux also uses OpenRC as its default process manager. Beyond Linux, OpenRC can also be used on BSD systems, such as GhostBSD.
Service Configuration Files
Taking Alpine Linux as an example, OpenRC's custom service configurations are placed in the /etc/init.d/ directory, where the configuration file name is the service name.
The syntax of OpenRC configuration files is similar to Shell scripts, making the learning curve low. An OpenRC configuration must declare at least three functions: start(), stop(), and status().
The rc-service command executes the corresponding function in a service configuration file. Suppose we have a some_service service. To start it, we run rc-service some_service start, which essentially executes the start() function in /etc/init.d/some_service. To simplify configuration writing, OpenRC also provides a default configuration file. Custom configurations can call default configuration functions or even directly inherit them.
Suppose our custom service doesn't have a background process after startup and only needs to execute some commands during system boot. We can define a custom start() function that, after running the desired commands, calls the default default_start function. This way, we don't need to define a status() function — we can rely on the default configuration's behavior to confirm whether startup is complete.
## /etc/init.d/some_service
## example of OpenRC service file
start()
{
# execute necessary commands
...
default_start
}
After the service starts, we can check its status:
$ rc-service some_service status
* status: started
Or:
$ rc-status
...
Runlevel: default
...
some_service [started]
...
Additionally, OpenRC supports user-defined command functions for services. See: Syntax of Service Scripts
Enabling Services at Boot
rc-service only executes service commands. To enable a service to start automatically at boot, use the rc-update command:
rc-update add some_service default
In systemd, if a custom service has dependencies on other services, they must be listed one by one in the configuration file. If any dependency changes, all related entries need to be adjusted.
OpenRC simplifies this problem elegantly by introducing the concept of runlevels. OpenRC processes services in different runlevels sequentially, and services within the same runlevel are started in alphabetical order. System-level services, such as networking, typically reside in the sysinit and boot runlevels. User-level services are placed in the default runlevel. This way, user services don't need to declare dependencies on system services.
The command above adds the some_service service to the default runlevel.
Summary
Although OpenRC's user base appears niche, in an online survey by Slant titled "What are the best Linux init systems?", OpenRC ranks first among voting participants. Meanwhile, systemd, used by mainstream Linux distributions, failed to make the top 5 in that survey.
Personally, I think OpenRC's design is excellent: simple and easy to learn, functional, and aligned with the Unix KISS philosophy.