Skip to main content

Linux

Understanding systemd Service Management & Unit File Troubleshooting

systemd is the init system behind nearly every modern Linux production server. This guide covers the commands, journal query patterns, dependency graph analysis, and unit file configuration fixes engineers need during production incidents.

4 minute read

Reading systemd Service Status Correctly

The first command in any systemd incident investigation is `systemctl status <service>`. Read the output in sequence: the `Loaded` line tells you whether the unit file exists and its enabled state. The `Active` line tells you whether the service is currently running, failed, or activating. The `Process` lines show the actual exit code and signal if the service crashed.

The five-line log excerpt shown by `systemctl status` is rarely enough. Follow it immediately with `journalctl -u <service> -n 100 --no-pager` to read the last 100 journal entries for that unit without triggering a pager. Add `--since "10 min ago"` to scope to the incident timeframe.

The `Loaded` line will show `enabled` or `disabled` — a service marked as disabled will not start automatically after a reboot. If a service is failing during incident response and this line shows disabled, manual start attempts with `systemctl start` will succeed but the service will not survive the next reboot.

Diagnosing Unit File Dependency Failures

Services that fail silently during boot often have dependency ordering problems. Run `systemctl list-dependencies <service>` to see the full dependency tree. Services marked with a red circle failed before your target service could start.

Use `systemd-analyze critical-chain <service>` to identify which unit is consuming the most startup time and causing downstream delays. This is particularly useful for database-dependent applications that race against PostgreSQL or MySQL during system boot.

The `After=` directive in a unit file only controls ordering, not hard dependencies. If your service must not start unless `postgresql.service` is fully active, use `Requires=postgresql.service` combined with `After=postgresql.service`. Using `After=` alone allows the service to attempt startup even if PostgreSQL failed.

The Most Common Unit File Misconfigurations

The single most common production unit file mistake is setting `Restart=always` without a `RestartSec=` delay. Without a delay, a crashing process will attempt to restart thousands of times per minute, exhausting file descriptors and polluting the journal with noise that obscures the actual error.

The correct production pattern for most application services is: `Restart=on-failure`, `RestartSec=5s`, `StartLimitIntervalSec=60`, `StartLimitBurst=3`. This allows three restart attempts within 60 seconds before systemd marks the service as failed and stops retrying.

Environment variable loading from files requires `EnvironmentFile=/path/to/file` in the `[Service]` section. A missing EnvironmentFile that is referenced without a leading `-` prefix will cause the service to fail silently without a useful error message. Prefix the path with `-` to make missing files non-fatal during development.

Related ShellMate resources

Questions about Understanding systemd Service Management & Unit File Troubleshooting

How do I check why a systemd service failed?

Run `systemctl status <service>` for the immediate status and exit code, then `journalctl -u <service> -n 200 --no-pager` for full log history. Look for exit code, signal (SIGSEGV, SIGKILL), and the last log line before termination.

How do I restart a systemd service correctly?

Use `systemctl restart <service>` for a full stop-and-start cycle. Use `systemctl reload <service>` if the application supports configuration reload via SIGHUP without a full process restart. Always follow with `systemctl status <service>` to verify the service is active.

What does Restart=always mean in a systemd unit file?

Restart=always means systemd will attempt to restart the service regardless of exit code or signal, including clean exits. Add RestartSec=5s to introduce a delay between attempts, and use StartLimitBurst to cap the number of retries before giving up.

Put the guide into practice

Organize hosts, choose a deliberate credential path, and keep SSH work visible in a focused desktop workspace.