1. Overview

Starting services in the right order is crucial for the smooth working of a Linux system. The init system, systemd, is responsible for starting, stopping, and managing services during the boot process. Therefore, it plays a central role in managing services on Linux systems.

In some cases, it’s useful to start one service after another specific service. We can achieve this by configuring service dependencies in systemd.

In this article, we’ll learn and apply service dependencies in systemd and configure a service to start after another specific service. We’ll also discuss the use of the After and Requires directives in the service unit file.

2. Understanding Systemd and Services

systemd is a modern init system and system manager for Linux systems. It replaces the traditional init system, SysVinit, and provides several improvements like parallel service startup, improved logging, and dynamic management of system processes.

During the boot process, systemd starts and manages processes known as services. These occupy three categories:

  • system services
  • user services
  • target units

System services begin at boot and are necessary for the system’s proper functioning. User services are services that are triggered on demand by a specific user. Target units are groups of services that are started and stopped together.

systemd provides a simple and consistent interface for controlling services through the systemctl command. This command can start, stop, restart, and check the current status of services. It also provides a way to configure service dependencies. It ensures that the services start in the correct order, avoiding potential conflicts and errors.

3. Configuring Service Dependencies

A simple way to configure service dependencies in Linux is to use systemctl and modify the service file. The service unit file lists all the dependencies along with information about the service.

Configuring a service to run after another service can be done with the edit subcommand of systemctl:

$ systemctl edit <service_name>.service

On running the above command, the service file opens in a text editor. We can add the After or Requires directives to define what service this service should be started after.

3.1. Understanding the After Directive

The After directive makes sure that the current service starts after the specified service.

In order to configure service1 to run after service2, we can add a line to the unit file of service1:

After=service2.service

To define multiple services in the After directive, we simply place the name of each service separated by a space:

After=service2.service service3.service service4.service

The current service will start once all specified services have run successfully.

3.2. Understanding the Requires Directive

The Requires directive specifies that the current service requires the specified service to be running. Similar to After, this directive stops the current service when the specified service stops.

Let’s check out the Requires directive format:

Requires=<service_name>.service

Thus, we can configure service1 to start service2 automatically by adding a line to service1’s unit file:

Requires=service2.service

It is critical to note that the After directive only sets a dependency between services and doesn’t guarantee the order of service start-up. If service1 and service2 have After for each other, they’ll cause a loop and systemd may fail to start.

Let’s now restart service1 to bring all the changes into effect:

$ systemctl restart service1.service

Importantly, the Requires directive takes precedence over the After directive, and it also creates a service ordering relationship.

4. Conclusion

In this article, we discussed the concept of service dependencies in systemd and how to configure a service to start after another specific service. In particular, we learned about the systemctl command and the use of the After and Requires directives in the service unit file.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.