December 5, 2026

systemd Restart=on-failure: Why a Crashing Service Gives Up After Half a Second

A factory machine with a red warning beacon lit and a glowing reset button

Adding Restart=on-failure to a unit file is the standard way to make a Linux service come back after a crash. Then the service crashes in a loop, and a few moments later systemd stops trying and marks it failed for good. That is not a bug. It is two settings meeting: how long systemd waits before each restart, and how many starts it allows in a time window. With the defaults, a service that fails instantly runs out of attempts in half a second. This post works through the numbers from the systemd documentation and the defaults shipped on Ubuntu 24.04 (systemd 255).

Video: Learn Linux TV on YouTube

What Restart= covers

The seven values

The systemd.service manual describes Restart= as configuring whether the service is restarted when the service process exits, is killed, or a timeout is reached. The default is no. The other values differ in which kinds of exit count, and the manual’s own table sets them side by side:

Exit cause always on-success on-failure on-abnormal on-abort on-watchdog
Clean exit code or signal ✓ ✓
Unclean exit code ✓ ✓
Unclean signal ✓ ✓ ✓ ✓
Timeout ✓ ✓ ✓
Watchdog ✓ ✓ ✓ ✓

The documentation’s table also has a row for termination by the out-of-memory killer, which restarts under always, on-failure and on-abnormal.

What “clean” means

A clean exit, in the manual’s definition, is exit code 0, or for services other than Type=oneshot, one of four signals: SIGHUP, SIGINT, SIGTERM or SIGPIPE. Exit statuses listed in SuccessExitStatus= also count as clean.

That definition has a consequence. A service killed with a plain kill, which sends SIGTERM, exited cleanly, so on-failure does not bring it back. A segmentation fault, SIGSEGV, is an unclean signal, and it does.

What never triggers a restart

The manual excludes one case outright: when the process dies as a result of systemd’s own operations, such as systemctl stop or restart, the service is not restarted. Stopping a service on purpose stays stopped. RestartPreventExitStatus= adds exit codes that should never cause a restart, for example a code the program uses to mean “the configuration is wrong, retrying will not help.”

Which value to use

The manual is unusually direct here: on-failure is “the recommended choice for long-running services.” For services that should be allowed to finish on their own without being restarted, it suggests on-abnormal.

Stopwatch

RestartSec and the default 100 milliseconds

The pause before each restart

RestartSec= sets how long systemd sleeps before restarting. The manual gives its default as 100ms. Ubuntu’s shipped /etc/systemd/system.conf shows the same value among its commented defaults:

#DefaultRestartSec=100ms
#DefaultStartLimitIntervalSec=10s
#DefaultStartLimitBurst=5

A tenth of a second suits a service that crashed once and will come straight back. For a service that fails the moment it starts, because a database is down or a config file is missing, it means five attempts in half a second.

The start limit

Five starts in ten seconds

The second half of the story is in the systemd.unit manual, which says that units started more than burst times within an interval time span are not permitted to start any more. The interval and the burst come from StartLimitIntervalSec= and StartLimitBurst=, and their defaults come from the manager configuration, which on this system means 10 seconds and 5 starts.

The same page is clear that automatic restarts count: the limit applies to all kinds of starts, “not just those triggered by the Restart= logic.” And once it is hit, units configured for Restart= “are not attempted to be restarted anymore.”

How fast the defaults run out

A simple model of those rules, for a service that fails immediately every time it starts:

Settings Outcome
Defaults: 100 ms delay, 5 starts in 10 s Blocked after 5 starts, about 0.5 s in
RestartSec=1, 5 in 10 s Blocked after 5 starts, about 5 s in
RestartSec=5, 5 in 10 s Never blocked: at most 2 starts fit in any 10 s
RestartSec=5, 5 in 60 s Blocked after 5 starts, about 25 s in

With the defaults, a service can burn through its whole allowance before a dependency like a network mount has even finished coming up. The usual fix is to raise RestartSec= so the attempts are spread out. Whether the service should eventually give up is a separate decision, made with the start limit.

Getting it running again

The unit manual names the reset: systemctl reset-failed flushes the restart counter for a service. After fixing the underlying problem, that or a manual start once the interval has passed brings it back.

Paper tickets

Backoff with RestartSteps

Growing delays

systemd 254 added exponential backoff. RestartSteps= sets how many steps it takes to grow the delay from RestartSec= up to RestartMaxDelaySec=. The manual’s own example:

RestartSec=10s
RestartSteps=4
RestartMaxDelaySec=160s

produces restart intervals of 10, 20, 40, 80 and then 160 seconds, staying at 160 after that. The manual gives the formula for the ratio between steps, (RestartMaxDelaySec ÷ RestartSec) raised to the power 1 ÷ RestartSteps, which here is 16 to the power ¼, or exactly 2. It suggests values between 3 and 5 for the step count.

Other numbers

The ratio does not have to be a round number. For a 5-second start growing to 60 seconds in 4 steps, the same formula gives a ratio of about 1.86 and delays of roughly 5, 9.3, 17.3, 32.2 and 60 seconds.

Backoff changes how the start limit plays out. Under the example settings above with a limit of 5 starts in 60 seconds, the model above never hits the limit: the starts land at 0, 10, 30, 70 and 150 seconds, and no 60-second window ever holds more than three of them. The service keeps trying indefinitely, just less and less often, which suits a dependency that may take minutes to come back.

Three exceptions worth knowing

Forcing and preventing

The table above has escape hatches in both directions. Alongside RestartPreventExitStatus=, which blocks restarts for listed codes, the manual describes RestartForceExitStatus=: services will always be restarted if the exit code or signal is listed there, whatever Restart= says. A program that exits with a specific code to ask for a restart, after rotating its own keys or reloading a large index, can use it without turning on Restart=always.

Oneshot services

Type=oneshot units, which run a task and finish, follow a stricter rule. The manual states that they are never restarted on a clean exit status, and that always and on-success are rejected for them. A backup script that succeeded is not run again in a loop. One that fails can still be retried with on-failure.

RestartMode=direct

Also added in systemd 254, RestartMode= changes how a restart looks to the rest of the system. The default, normal, passes through a failed or inactive state between attempts. With direct, the service goes straight back to activating during an automatic restart, skipping that state. The manual’s stated purpose: a dependency can fail temporarily without the units that depend on it being told, and failing in turn. OnFailure= handlers are skipped for those restarts too, so an alerting unit attached to OnFailure= stays quiet until the service gives up for real.

Stones in a stream

A unit file that puts it together

[Unit]
Description=Flaky app
StartLimitIntervalSec=60
StartLimitBurst=5

[Service]
ExecStart=/usr/local/bin/flaky-app
Restart=on-failure
RestartSec=5
RestartSteps=4
RestartMaxDelaySec=60

[Install]
WantedBy=multi-user.target

The start-limit settings go in [Unit], not [Service], which the unit manual confirms by listing them under its [Unit] section options. Putting them in [Service] is a common mistake.

Checking it before installing

systemd-analyze verify parses a unit file without loading it. On the file above it reported no problems. A copy with a deliberate typo, Restart=sometimes, produced:

bad.service:8: Failed to parse service restart specifier, ignoring: sometimes

The key word is “ignoring.” A unit file with a misspelled restart value still loads; systemd simply drops the line and falls back to the default, no. The service then never restarts, and nothing fails loudly enough to notice until the first crash. Running verify after every edit catches it.

When restarting is the wrong answer

Crash loops hide causes

Automatic restarts keep a service up through rare, transient faults. They also turn a deterministic failure into a log full of identical start attempts. journalctl -u flaky-app shows each one, and the first failure in a burst is usually the one that explains the rest.

Dependencies are better than retries

When the reason for failing at boot is ordering, a service that starts before the network or a mount is ready, the better fix is a dependency such as After= and Wants= on the right target, so systemd starts it when the thing it needs is there. Restart settings are for failures that cannot be predicted. Ordering is for the ones that can.

Sources

  • systemd.service(5), “Restart=”, “RestartSec=”, “RestartSteps=”, “RestartMaxDelaySec=” (latest), freedesktop.org: https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html
  • systemd.unit(5), “StartLimitIntervalSec=, StartLimitBurst=” (latest), freedesktop.org: https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *