How Restart= interacts with exit causes

The systemd.service manual provides a table that maps exit causes to each Restart= value. A clean exit code or signal only triggers restarts under on-success and always. An unclean exit code restarts the service only with always or on-failure. Termination by an uncaught signal restarts under always, on-failure, on-abnormal, and on-abort. Timeouts and watchdog expirations follow similar patterns. Understanding these distinctions prevents services from restarting when they should stay stopped.

On a Raspberry Pi, power or SD card issues often produce unclean exits or core dumps. Setting Restart=on-failure catches these cases without restarting after an intentional shutdown. The on-abnormal setting is useful when you want restarts only after signals or timeouts but not after a clean exit code of zero.

  • Clean exit: restarts only with always or on-success
  • Unclean exit code: restarts with always or on-failure
  • Signal termination: restarts with always, on-failure, on-abnormal, or on-abort
  • Timeout or watchdog: restarts with always, on-failure, on-abnormal, or on-watchdog

Combining restart settings with ordering directives

Restart behavior works together with Before=, After=, Wants=, and Requires= in the [Unit] section. A service that must start after the network is ready uses After=network-online.target and Wants=network-online.target. If that service also uses Restart=on-failure, systemd will re-evaluate ordering each time it restarts. The Before= directive delays dependent units until the current unit reports it is fully started.

When a restarted service takes time to become ready, dependent units may start too early. Adding a short RestartSec= value gives the service breathing room. Raspberry Pi projects that rely on attached hardware often need both After= and a restart policy so that a transient USB or GPIO failure does not leave the whole stack in a broken state.

  • Place After= and Wants= in the [Unit] section, not [Service]
  • RestartSec= adds a delay between restart attempts
  • Before= ensures dependent units wait for full startup

Logging and journalctl after restarts

Every restart writes new entries to the journal. Use journalctl -u yourservice.service to view the history of starts, stops, and failures. Adding the -f flag follows new output in real time. When a service restarts repeatedly, the journal shows the exact exit code or signal that triggered each attempt. This information is essential for deciding whether the current Restart= setting is appropriate.

Raspberry Pi OS stores the journal on disk by default. Long-running services that restart frequently can fill the journal partition. Limiting the number of restarts with StartLimitIntervalSec= and StartLimitBurst= protects storage while still allowing recovery from occasional crashes.

  • journalctl -u name.service shows restart history
  • journalctl -u name.service -f follows live output
  • StartLimit settings prevent restart storms from filling the journal

User services versus system services

User services live in /etc/systemd/user or ~/.config/systemd/user and run under the logged-in user. They inherit the user's environment and do not require root. System services in /etc/systemd/system run as root or a dedicated system user. Restart policies behave the same way in both cases, but user services stop when the user logs out unless lingering is enabled.

For a small Raspberry Pi project that should survive reboots without a logged-in user, install the unit as a system service. If the project only needs to run while a particular user is active, a user service with Restart=on-failure is often simpler and safer.

  • System units: /etc/systemd/system, run as root or system user
  • User units: /etc/systemd/user, run under the owning user account
  • Enable lingering for user services that must survive logout

Practical steps to test a restart policy

Create the unit file, run daemon-reload, then enable and start the service. Simulate a crash by sending SIGKILL or by causing the program to exit with a non-zero code. Observe whether the service restarts and how long it waits. Check the journal for the exact cause recorded by systemd. Adjust Restart= and RestartSec= until the observed behavior matches the intended recovery strategy.

After changes, always run systemctl daemon-reload before restarting the service. Failure to reload leaves the old unit definition active even though the file on disk has been edited.

  • Test with intentional crashes and watch the journal
  • Reload the daemon after every unit file edit
  • Verify behavior across a full reboot