Random Musings

Sporadic thoughts on tech, economics, business, finance and trading

What is the Difference Between Wants and Requires directives in SystemD unit files

,

The Wants and Requires directives in SystemD unit files define dependencies between services, but they behave differently in terms of how they enforce and handle dependencies.


Requires=

  • Purpose: Specifies a hard dependency on another unit. If the unit listed in Requires= is not available, the current unit will fail to start.
  • Behavior:
  • If the unit specified in Requires= fails to start, the current unit will not start either.
  • When the current unit stops, any unit listed in Requires= is also stopped automatically.
  • Use case: For dependencies that are critical for the service to function. For example, a database service might Require a network service to be up and running. Example:
  [Unit]
  Requires=network.target

In this case, the service won’t start unless the network.target is active.


Wants=

  • Purpose: Specifies a weak dependency on another unit. The system will try to start the unit listed in Wants=, but it is not a failure if that unit cannot start.
  • Behavior:
  • If the unit specified in Wants= fails to start, the current unit will still proceed to start.
  • When the current unit stops, the Wants= dependency is not stopped automatically.
  • Use case: For optional dependencies that enhance functionality but are not critical. For example, a service might Want a logging service to be active, but it can run without it. Example:
  [Unit]
  Wants=remote-fs.target

In this case, the service will try to start remote-fs.target, but it won’t fail if remote-fs.target cannot be activated.


Key Differences:

AspectRequires=Wants=
Dependency StrengthHard (mandatory)Soft (optional)
Effect of FailureIf the dependency fails, the unit fails to start.If the dependency fails, the unit starts anyway.
Stopping BehaviorStopping the unit stops the dependency.Stopping the unit does not stop the dependency.

Summary:

  • Use Requires= for must-have dependencies that are critical for the service to work.
  • Use Wants= for nice-to-have dependencies that are optional but can enhance functionality.