Cobalt Field Notes

Ordering is not dependency in systemd

· 2 min read

Most of the confusing systemd behaviour I have run into comes down to one distinction: the directives that decide whether another unit is started are separate from the ones that decide when. Mixing them up produces units that work on one machine and race on the next.

Two independent axes

Wants= and Requires= are requirement dependencies. Starting a unit with Wants=b.service also queues a start job for b.service. Requires= is stronger: if b.service is explicitly stopped or restarted, the depending unit is stopped or restarted with it. A failed start of b only prevents the depending unit from starting when an ordering dependency is set as well.

After= and Before= only order jobs that are already part of the same transaction. After=network-online.target on its own does not cause that target to be reached; it only says “if both are being started, start me second”. That is why the documentation asks units that need the network to pair it with Wants=network-online.target.

Reading the graph

Two commands answer most questions:

systemctl list-dependencies --after nginx.service
systemctl show -p Wants -p Requires -p After nginx.service

The first prints the ordering tree; without --after it prints the requirement tree instead. The second prints the resolved values, including the dependencies systemd adds implicitly: a service gets Requires= and After= on sysinit.target, and After=basic.target, unless it sets DefaultDependencies=no.

For slow boots, systemd-analyze critical-chain shows the chain of units that held up a target, with the time each one took to become active.

A checklist for new units

Restart loops

Restart=on-failure hides crashes from a quick glance: the unit flips between activating and failed and can look healthy at the moment you check. StartLimitIntervalSec= and StartLimitBurst= in the [Unit] section decide when systemd gives up, after which systemctl status reports start-limit-hit. A deploy script that wants to know whether a service came up should wait a couple of seconds and ask again.