TExcelTAD
Home › Linux Server Administration › Targets, Dependencies & Boot Order

Targets, Dependencies & Boot Order

systemd boots fast because it starts units in parallel wherever nothing forbids it. Your unit files say what is forbidden. Two separate kinds of relationship do that, and mixing them up is the most common unit-file bug.

Requirement vs ordering

Kind Keys Question it answers
Requirement Wants=, Requires=, BindsTo=, PartOf=, Requisite= "If A is started, must B be started too? What if B fails or stops?"
Ordering After=, Before= "If both are being started, which one first?"

They are independent. Requires=postgresql.service alone starts PostgreSQL at the same time as your app, so the app may still race it and fail to connect. After=postgresql.service alone only orders: if nothing else starts PostgreSQL, it won't be started. You almost always want a pair:

ini
[Unit]
Wants=postgresql.service
After=postgresql.service
Requirement If B fails to start If B stops later
Wants=B A starts anyway (weak, the recommended default) A keeps running
Requires=B A is not started A is stopped too, if B is stopped explicitly
BindsTo=B A is not started A is stopped whenever B goes away, for any reason
PartOf=B (no start effect) stopping or restarting B stops or restarts A
Requisite=B A fails at once unless B is already active –

PartOf= is handy for a group of helpers: PartOf=app.target on each worker, and systemctl restart app.target restarts them all.

Targets

A target is a unit with no process of its own; it is a named point in the boot. The important ones:

Target Reached when Old runlevel
sysinit.target, basic.target early boot: filesystems, swap, sockets –
network.target the network management stack is up (not that the network works) –
network-online.target the network is configured and usable (as far as the network manager knows) –
multi-user.target a normal non-graphical server is up 3
graphical.target a desktop login is up 5
rescue.target / emergency.target single-user repair modes 1 / –

A server's default is multi-user.target (systemctl get-default). A service that needs to connect elsewhere at start (a database on another host, a mount from NFS) should use Wants=network-online.target + After=network-online.target; ordering after network.target only guarantees that the network stack was started, not that an address was configured.

When the graph is wrong

systemd-analyze verify builds the start transaction for the unit, so it catches cycles and missing hard dependencies. Here the developer of api.service and the developer of worker.service each ordered their unit after the other's:

bash · try it yourself
cat > api.service <<'EOF'
[Unit]
Description=API
Requires=worker.service
After=worker.service
[Service]
ExecStart=/usr/bin/sleep infinity
EOF
cat > worker.service <<'EOF'
[Unit]
Description=Worker
After=api.service
[Service]
ExecStart=/usr/bin/sleep infinity
EOF
systemd-analyze verify ./api.service
echo "exit code: $?"
worker.service: Found ordering cycle on api.service/start
worker.service: Found dependency on worker.service/start
worker.service: Unable to break cycle starting with worker.service/start
Requested transaction contains an unfixable cyclic ordering dependency: Transaction order is cyclic. See system logs for details.
api.service: Failed to create api.service/start: Transaction order is cyclic. See system logs for details.
exit code: 1

On a live system systemd "breaks" such a cycle by dropping one of the jobs (and logs it), so the boot carries on with a unit silently not started. A missing hard dependency is caught the same way:

bash · try it yourself
cat > report.service <<'EOF'
[Unit]
Requires=postgresql.service
After=postgresql.service
[Service]
Type=oneshot
ExecStart=/usr/bin/true
EOF
systemd-analyze verify ./report.service
echo "exit code: $?"
report.service: Failed to create report.service/start: Unit postgresql.service not found.
exit code: 1

A model of the start order

Ordering forms a directed graph; systemd starts every unit whose After= predecessors are done, many at once. This model computes the "waves" of parallel starts for a small server, the same way systemd-analyze critical-chain reasons about it:

python · try it yourself
after = {   # unit -> units it is ordered After=
    "local-fs.target": [],
    "network.target": [],
    "network-online.target": ["network.target"],
    "postgresql.service": ["local-fs.target", "network.target"],
    "redis.service": ["network.target"],
    "inventory.service": ["network-online.target", "postgresql.service", "redis.service"],
    "nginx.service": ["network.target"],
    "multi-user.target": ["inventory.service", "nginx.service"],
}

done, wave = set(), 1
while len(done) < len(after):
    ready = sorted(u for u, deps in after.items() if u not in done and all(d in done for d in deps))
    if not ready:
        print("cycle among:", sorted(set(after) - done)); break
    print(f"wave {wave}: {', '.join(ready)}")
    done.update(ready); wave += 1
wave 1: local-fs.target, network.target
wave 2: network-online.target, nginx.service, postgresql.service, redis.service
wave 3: inventory.service
wave 4: multi-user.target

nginx.service starts in the second wave, long before the API it proxies to is ready. That is fine: Nginx returns 502 until the backend answers, and adding After=inventory.service to Nginx would only slow the boot. Order units only where one really cannot work without the other being started.

On a real server, systemd-analyze shows where boot time went (sample session):

bash
$ systemd-analyze
Startup finished in 2.115s (kernel) + 6.904s (userspace) = 9.020s
multi-user.target reached after 6.871s in userspace.
$ systemd-analyze critical-chain inventory.service
inventory.service @5.212s
└─postgresql.service @3.010s +2.190s
  └─network.target @2.981s
    └─systemd-networkd.service @2.402s +577ms
$ systemd-analyze blame | head -3
2.190s postgresql.service
1.204s cloud-init.service
 577ms systemd-networkd.service

In critical-chain, @ is when the unit became active and + is how long it took to start.

Test yourself

A unit has Requires=db.service but no After=. What happens at boot?

  1. The unit waits for db.service
  2. Both start in parallel; the unit may start before the database is ready
  3. db.service is not started
  4. systemd refuses the unit

Why prefer Wants= over Requires= in most units?

  1. It is faster to type
  2. A failure of the wanted unit doesn't prevent your unit from starting, so one broken dependency doesn't cascade
  3. Requires is deprecated
  4. Wants also orders the units

Your service connects to a remote database at startup. Which pair should it use?

  1. After=network.target
  2. Wants=network-online.target and After=network-online.target
  3. Requires=multi-user.target
  4. Before=network.target