In this lesson, you will learn how to enforce mutual exclusion between services using Conflicts and Breaks directives to prevent incompatible processes from running simultaneously.
Understanding Service Conflicts
In a complex Linux environment, you occasionally have two services that cannot coexist. For example, you might have two different network management daemons—NetworkManager and systemd-networkd—that both attempt to control the same hardware interfaces. If both run at once, they will fight over resources, causing unpredictable instability.
systemd provides two primary directives to manage these situations: Conflicts and Breaks.
The Conflicts Directive
When you add Conflicts=service-name.service to a unit file, you tell systemd that these two services are logically incompatible. If you attempt to start a service that conflicts with one already running, systemd will automatically stop the existing service before starting the new one.
For instance, if your custom web-server-A.service cannot run while web-server-B.service is active, you would add this to your unit file:
[Unit]
Description=My Web Server A
Conflicts=web-server-B.service
The Breaks Directive
Breaks acts like Conflicts, but with a more aggressive "shutdown" behavior. While Conflicts merely ensures they aren't both running, Breaks explicitly shuts down the conflicting unit the moment the current unit is activated. It is primarily used to ensure that a specific version or type of service is replaced entirely by the new unit, effectively "breaking" the existing link to the old service.
Think of these directives as a built-in referee. Instead of worrying about manual service management or shell scripts that check for process IDs, you declare the incompatibility at the unit level. systemd handles the stop-and-start sequence, ensuring that the transition happens safely without leaving you with two competing processes fighting for the same port or configuration file.
Key Takeaway: Use the Conflicts and Breaks directives to explicitly define mutually exclusive services, allowing systemd to automatically manage the stop-and-start transitions required to keep your system stable.