June 8, 2025
Introduction to OpenRC: Managing Services with Ease

Introduction to OpenRC: Managing Services with Ease

OpenRC is a dependency-based init system used by various Linux distributions. It is designed to provide a lightweight, efficient, and flexible service management system. OpenRC does not depend on heavy or complex tools like systemd, making it a great choice for systems where simplicity and control are paramount. OpenRC is used to start, stop, restart, enable, and disable services, allowing for easy management of system processes.

In this article, we’ll walk you through how to create, start, stop, restart, enable, and disable services with OpenRC.

What is OpenRC?

OpenRC is a service manager that executes system services and manages service dependencies on startup and shutdown. It is commonly used in lightweight Linux distributions like Alpine Linux, and is a core component of systems designed for simplicity and speed. OpenRC scripts are often written in shell script format and are stored in /etc/init.d/, the standard location for init scripts.

While OpenRC doesn’t have the extensive complexity of systemd, it provides essential features like dependency management, parallel execution of services, and reliable startup/shutdown routines.

OpenRC Service Basics

Before diving into how to manage services with OpenRC, it’s important to know that services in OpenRC are controlled using the rc-service command. Additionally, you’ll interact with the OpenRC service directory (/etc/init.d/) where service scripts are located.

Basic Commands for Managing Services with OpenRC:

  • Start a Service: rc-service start
  • Stop a Service: rc-service stop
  • Restart a Service: rc-service restart
  • Enable a Service: rc-update add
  • Disable a Service: rc-update del

Now, let’s break down each command and demonstrate how to use them.

Creating a New Service in OpenRC

To create a service in OpenRC, you need to write an init script. OpenRC uses shell scripts located in the /etc/init.d/ directory to manage services. These scripts define how services are started, stopped, and managed.

Create the Service Script

For example, let’s create a simple service script for a service called my_service. We will use a basic shell script that runs a program located at /usr/local/bin/my_program.

Open a terminal and create the service script:

sudo nano /etc/init.d/my_service

Add the following content to the script (this is just a basic template):

#!/sbin/openrc-run

command=/usr/local/bin/my_program
command_args="--option value"
pidfile=/run/my_service.pid

depend() {
    need net
    before local
}

start() {
    ebegin "Starting My Service"
    start-stop-daemon --start --pidfile $pidfile --exec $command -- $command_args
    eend $?
}

stop() {
    ebegin "Stopping My Service"
    start-stop-daemon --stop --pidfile $pidfile
    eend $?
}

Here, the script specifies a few important things:

  • command: The program to run.
  • command_args: Arguments passed to the command.
  • pidfile: The location of the PID file.
  • The depend() function defines dependencies for the service.
  • The start() and stop() functions define how to start and stop the service.

Save and close the file.

Make the Script Executable

You’ll need to make the script executable:

sudo chmod +x /etc/init.d/my_service

Starting, Stopping, and Restarting Services

With your service script in place, you can start, stop, and restart your service.

Start a Service

To start a service, use the rc-service command:

sudo rc-service my_service start

This will execute the start() function in the my_service script, which will run the command defined in the script.

Stop a Service

To stop a service, use the rc-service command:

sudo rc-service my_service stop

This will execute the stop() function in the script and terminate the service.

Restart a Service

To restart a service, use the rc-service command:

sudo rc-service my_service restart

This will stop and then start the service again, ensuring it is reloaded with any new configurations or updates.

Enabling and Disabling Services

One of OpenRC’s useful features is the ability to enable and disable services to run automatically at startup.

Enable a Service

To enable a service, use the rc-update add command. This adds the service to the appropriate runlevel (the default is usually default):

sudo rc-update add my_service default

This will create symlinks in the /etc/init.d/ directory to ensure that the service starts automatically at boot. OpenRC will start the service according to its runlevel configuration.

Disable a Service

To disable a service, use the rc-update del command:

sudo rc-update del my_service default

This removes the service from the specified runlevel, preventing it from starting automatically during system boot.

Managing Service Dependencies

OpenRC has a simple and effective mechanism for managing service dependencies. The depend() function in a service script defines what other services must be running before this service starts. Here’s an example of a service dependency:

depend() {
    need net
    before local
}

This tells OpenRC that my_service needs the net service to be started before it and should be started before the local service.

Viewing Service Status

You can check the status of a service at any time using the rc-service command with the status option:

rc-service my_service status

This will give you the current status of the service, such as whether it is running or stopped.

Conclusion

OpenRC provides a simple and powerful way to manage services in a Linux environment. While it might not have the complexity of systemd, it is lightweight, flexible, and highly efficient for systems where control over services is crucial. By using the rc-service and rc-update commands, you can easily create, start, stop, restart, enable, and disable services, as well as manage service dependencies.

With this guide, you should be equipped to handle basic OpenRC service management tasks on your system.

Leave a Reply

Your email address will not be published. Required fields are marked *