Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Systemd and Logging (Read-Only)

systemd unit dependencies

Understand systemd dependency directives: Wants, Requires, BindsTo, Conflicts and the separate ordering options After and Before, with the common pitfalls.

systemd starts units in parallel, so it needs two independent kinds of information from each unit file: what else must be running (dependencies) and who goes first (ordering). The key point of this page is that these are separate settings. Requires=db.service does not make the database start first, and After=db.service does not make it start at all.

Dependencies live in the [Unit] section. This is unit-file configuration, so it is read-only in the sandbox; the examples work with sample unit files and compute a start order, which is what systemd does internally.

INI
[Unit]
Description=Web application
Wants=redis.service
Requires=postgresql.service
After=postgresql.service redis.service network-online.target

Try it

Bash Dependency and ordering are different fields

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Compute a start order with tsort (simulated)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

All directives take a space-separated list of unit names.

Name Description
Wants= Weak dependency: start the listed units too, but carry on if they fail. The recommended default.
Requires= Strong dependency: if a required unit fails to start, this unit is not started; if it is stopped later, this one is stopped too.
Requisite= Like Requires= but the other unit must already be active, or this one fails at once.
BindsTo= Like Requires= and additionally stops this unit as soon as the other one stops for any reason (even a crash).
PartOf= Stop or restart requests for the other unit propagate to this one, not the other way.
Conflicts= The two units cannot run together; starting one stops the other.
After= Ordering: start this unit only after the listed ones have finished starting.
Before= The opposite ordering: listed units start only after this one.
Upholds= Newer, restarts the listed units whenever they are not running.
OnFailure= Units to activate when this one fails (for example a notification).

Errors and edge cases: an ordering cycle (A after B after A) makes systemd drop one of the jobs and log "Found ordering cycle". A Requires= on a unit that does not exist makes the start fail with "Unit not found".

Usage notes

  • Pair them. To depend on a database and wait for it, write both Requires=postgresql.service and After=postgresql.service. Either alone is a common bug: without After=, both start at the same time.
  • Prefer Wants=. It lets your service boot when an optional piece is broken. Use Requires= only when the service really cannot work without the other.
  • "Started" is not "ready". After= waits for the other unit to be considered started, which for Type=simple is only the moment its process begins. A service that needs a port to be open should be Type=notify or retry connecting.
  • Network: After=network.target only means networking software has begun. Use Wants=network-online.target plus After=network-online.target when you need a configured address.
  • Targets organize boot. WantedBy=multi-user.target in [Install] is the reverse of Wants=: see Targets.
  • Inspect the graph with systemctl list-dependencies UNIT (add --reverse for who depends on it) and systemd-analyze critical-chain UNIT; see systemd-analyze.
  • Stop order is the reverse of start order, so After= also controls shutdown: the dependent stops first.
  • Drop-ins (systemctl edit) can add dependencies without touching the packaged file; an empty After= resets the list.