System Services
Alpine Linux uses the OpenRC system for starting, stopping, and managing system services. This page explains how to work with OpenRC commands.
OpenRC Commands
Since Alpine Linux uses OpenRC instead of systemd, different commands are used to interact with system services. Instead of using the systemctl command, two separate commands are used:
- rc-service, which allows services to be managed manually, and
- rc-update, which allows services to be added or removed from runlevels.
For now, let’s focus on the rc-service command. In these examples, we’re going to use a fictional system service named foo. However, the command patterns below would apply to any service on the system by simply swapping the name of the service with foo in these examples. Typically, only the root user can start or stop services, so the commands to perform these steps would need to be prefixed with the doas command when running as a regular user.
Starting a Service
To start a service that has not yet been started, run:
rc-service foo start
Restarting a Service
If a service is already running but needs to be restarted, use:
rc-service foo restart
Stopping a Service
To stop a running service, execute:
rc-service foo stop
Checking Service Status
Not all services support status checks. However, many services do implement a status command and will respond to:
rc-service foo status
Listing all Running Services
A quick way to list all currently running services is to run:
rc-status
Runlevels
OpenRC automatically starts and stops configured services according to the system’s runlevel. Runlevels are useful on server systems for troubleshooting purposes, and it’s possible to add custom runlevels with OpenRC. However, the main runlevels that are available by default for configuring services on Alpine Linux are:
Runlevel | When Executed |
---|---|
sysinit | Early system startup (for the most core services) |
boot | Services involved with mounting file systems (possibly including network) |
default | After the sysinit and boot stages are complete |
shutdown | As the system is rebooting or powering off |
Most services are added to the default runlevel in Alpine Linux. However, a service can be added to (or removed from) another runlevel by specifying that runlevel in place of the word default in the following examples. We’ll continue to use the example service foo for the service name. Note that the default at the end of each of the first two commands can be omitted, since the default runlevel is implied (by default… hence the name).
Enabling a Service
To enable a service so that it starts as the system boots, run:
rc-update add foo default
Disabling a Service
To remove a service so that it isn’t started when the system boots, run:
rc-update del foo default
Listing All Services
To list the runlevel for all services that are configured for automatic execution, run:
rc-update
with no arguments. Note that this command lists only services that have been added to at least one runlevel.
To see all available services on the system, even ones that haven’t been added to a runlevel, run:
ls /etc/init.d
Log Files
Since Alpine Linux doesn’t use systemd, it also doesn’t use the binary log file format (the journal) that systemd imposes. Thus, instead of using journalctl to view logs, standard text file commands are instead run on files in the /var/log directory. Note that you generally need to be root to run these commands, which implies using doas when run as a regular user.
To see available log files, run:
ls /var/log
Some programs are configured to write to dedicated log files. To see the log output of these programs, substitute the log file name for /var/log/messages in the following examples. Programs that do not write to their own log files normally write to /var/log/messages instead.
Displaying Kernel Logs
To see kernel logs, run the command:
dmesg
On the console, most the output will scroll of the screen. To be able to read the whole output, pipe it through a pager like less:
dmesg | less
To search for a key word, use the grep command (this example searches for the word input):
dmesg | grep input
Displaying Application Logs
See the note above about substituting the file name for applications that write their own log files. In the general case, log messages can be viewed by running:
cat /var/log/messages
The same techniques for piping the output through a pager or grep can be used as shown above to allow the whole file to be used or to allow searching for a keyword.
Sometimes it is useful to watch the log messages as they occur, especially when debugging a server application:
tail -f /var/log/messages
To stop the display of log output and return to the command prompt, press CTRL+C.