systemd does exactly what the unit file says. Most surprises are in what it doesn't say.
The mental model
systemd is PID 1 on most Linux servers. It reads unit files, keeps them in memory, starts processes in their own cgroups, restarts them according to policy, and sends their stdout and stderr to the journal.
Unit files are read from three places. The higher one wins:
/etc/systemd/system/: your units and overrides./run/systemd/system/: units generated at runtime./lib/systemd/system/(or/usr/lib/...): units shipped by packages. Don't edit these.
A minimal service
[Unit] Description=API After=network-online.target Wants=network-online.target [Service] User=api WorkingDirectory=/opt/api EnvironmentFile=/etc/api.env ExecStart=/opt/api/bin/server --port 8080 Restart=on-failure RestartSec=5s LimitNOFILE=65535 [Install] WantedBy=multi-user.target
Daily commands
sudo systemctl daemon-reload # after any unit file change sudo systemctl enable --now api # start now and at boot systemctl status api # state, main PID, last log lines journalctl -u api -n 100 --no-pager # recent logs journalctl -u api -f # follow journalctl -u api --since "1 hour ago" systemctl cat api # unit plus every drop-in, merged systemctl list-units --failed
Override, don't edit
sudo systemctl edit nginx
This creates /etc/systemd/system/nginx.service.d/override.conf, survives package upgrades, and reloads systemd when you save. List settings such as ExecStart= add to the original value, so reset them first:
[Service] ExecStart= ExecStart=/usr/sbin/nginx -g 'daemon off;'
Restart behaviour
Restart=defaults tono.on-failurecovers non-zero exits, unclean signals, timeouts and watchdog failures.- The start rate limit defaults to 5 starts in 10 seconds (
StartLimitBurst=,StartLimitIntervalSec=in[Unit]). After that the unit stays failed untilsystemctl reset-failed. systemctl stopsends SIGTERM, then SIGKILL afterTimeoutStopSec=, which defaults to 90 seconds.
Gotchas
/etc/security/limits.confdoesn't apply to services. UseLimitNOFILE=and similar settings.- Shell profiles aren't sourced. Use
Environment=,EnvironmentFile=and absolute paths. Type=simplecounts the service as started as soon as the process is forked. Ordering withAfter=doesn't wait for the app to become ready unless it usesType=notify.restartandreloadare different.reloadrunsExecReload=, often a SIGHUP, and fails if the unit doesn't define one.
Common wrong fix
Editing the package's unit in /lib/systemd/system/ directly. It works until the next apt upgrade or dnf update overwrites the file, and the change silently disappears, usually during a maintenance window. Put changes in a drop-in with systemctl edit, and check the result with systemctl cat.