Skip to content
Loopedin Labs

systemd services

Units, restarts, drop-ins and the journal: how your service actually runs.

Mini Rush · practice

Three questions before you read

Guessing first makes the answers stick. No sign-in.

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:

  1. /etc/systemd/system/: your units and overrides.
  2. /run/systemd/system/: units generated at runtime.
  3. /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 to no. on-failure covers 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 until systemctl reset-failed.
  • systemctl stop sends SIGTERM, then SIGKILL after TimeoutStopSec=, which defaults to 90 seconds.

Gotchas

  • /etc/security/limits.conf doesn't apply to services. Use LimitNOFILE= and similar settings.
  • Shell profiles aren't sourced. Use Environment=, EnvironmentFile= and absolute paths.
  • Type=simple counts the service as started as soon as the process is forked. Ordering with After= doesn't wait for the app to become ready unless it uses Type=notify.
  • restart and reload are different. reload runs ExecReload=, 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.