Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Systemd and Logging (Read-Only)

systemd restart policies

Control how systemd restarts crashed services: Restart= values, RestartSec, start limits, exit status rules and the settings that stop restart loops.

A service that crashes at 3 a.m. should come back without a human. systemd handles this with the Restart= setting in the [Service] section: after the main process ends, systemd decides from how it ended whether to start it again. RestartSec= adds a pause, and start limits stop a service that fails instantly from looping forever.

This is configuration for unit files, so it is read-only in the sandbox (no systemd). The examples simulate the decision table and the backoff arithmetic so you can reason about a setting before you deploy it.

INI
[Service]
ExecStart=/usr/local/bin/worker
Restart=on-failure
RestartSec=5s

[Unit]
StartLimitIntervalSec=60
StartLimitBurst=4

Try it

Bash Which exits trigger a restart (simulated table)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Start limit arithmetic (simulated)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
Restart=no|on-success|on-failure|on-abnormal|on-abort|on-watchdog|always
RestartSec=5s
RestartSteps=N    RestartMaxDelaySec=1min
SuccessExitStatus=3 4 SIGTERM
RestartPreventExitStatus=2
StartLimitIntervalSec=10s    StartLimitBurst=5    StartLimitAction=none
Name Description
Restart=no Default. Never restart.
Restart=on-success Restart only after a clean exit (status 0, or a status listed in SuccessExitStatus=).
Restart=on-failure Restart after a non-zero exit, a fatal signal, a timeout or a watchdog failure. The usual choice for services.
Restart=on-abnormal Restart after a signal, timeout or watchdog failure, but not after an ordinary non-zero exit.
Restart=on-abort Restart only after an uncaught signal.
Restart=always Restart whatever the reason, except when you stop the unit yourself.
RestartSec= Wait before restarting (default 100 ms).
RestartSteps=, RestartMaxDelaySec= Grow the delay gradually from RestartSec= up to the maximum (newer systemd).
SuccessExitStatus= Extra exit codes or signals that count as success.
RestartPreventExitStatus= Exit codes or signals after which the service is never restarted.
StartLimitIntervalSec=, StartLimitBurst= Allow at most Burst starts in the interval (in [Unit]; defaults 10 s and 5).
StartLimitAction= Optional action when the limit is hit, such as reboot-force (use sparingly).

Errors and edge cases: when the limit is hit, the unit enters the failed state with result start-limit-hit and systemd stops trying; reset it with systemctl reset-failed UNIT.

Usage notes

  • on-failure is the safe default for daemons: it restarts crashes but lets a program that exits 0 on purpose stay stopped.
  • always hides bugs. A service that quits normally and is relaunched forever is rarely what you want; use it for processes that are meant to run until stopped.
  • Always set RestartSec=. The default 100 ms means a crash loop burns CPU and floods the journal until the start limit trips.
  • Start limits live in [Unit], not [Service]; putting them in the wrong section is silently ignored by older versions. To disable the limit, set StartLimitIntervalSec=0.
  • Type=oneshot units cannot use Restart=always; use on-failure or a timer.
  • Restart is not recovery. A program that fails because its database is down will just fail again; pair Restart= with dependencies from Unit dependencies and a sensible delay.
  • Check what happened with systemctl status UNIT (shows Main PID, exit status, restart counter) and systemctl show UNIT -p NRestarts.
  • Manual stops are respected. systemctl stop never triggers a restart, even with Restart=always.