4. systemd: services, timers, journal
One-liner. Units describe what to run; systemd decides when, how often, and what to do on failure.
Memorize
- Unit types:
.service,.timer,.socket,.target,.mount,.path. - Dependencies:
Requires=(hard),Wants=(soft),After=/Before=(ordering only — ordering is not dependency). Restart=on-failure,RestartSec=,StartLimitBurst=guard crash loops.- Socket activation: systemd owns the socket and starts the service on first connection.
systemctl daemon-reloadafter editing units;enable --nowfor boot + start.- journald:
journalctl -u name -f,-b,-p err,--since,-o json. - Hardening knobs:
NoNewPrivileges,ProtectSystem=strict,PrivateTmp,ProtectHome,DynamicUser.
Trap. Writing After=network.target and assuming networking works — the network may be up without DNS or routes. Do this. Depend on what you actually need (network-online.target, a health check, or retry in the app).
Check
What is the difference between Requires= and After= in a systemd unit?
Why
Ordering says "when", dependency says "whether it must be there"; you usually need both.