Interview Q&A / Linux & Shell

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How do you make systemd restart a service automatically if it crashes?

SHORT ANSWER

Set Restart=on-failure (or always) in the [Service] section, usually with RestartSec= to add a delay. systemd then restarts the process after a crash, and StartLimitBurst and StartLimitIntervalSec stop endless crash loops.

Explain it simply

A guard who stands up again every time he falls, but who stops trying if he has fallen five times in a minute.

In the [Service] section:

ini
Restart=on-failure
RestartSec=5

Common values of Restart=:

  • no: never (the default).
  • on-failure: restart on a non-zero exit, a signal, or a timeout, but not on a clean exit.
  • always: restart no matter how it stopped, even after a clean exit.
  • on-abnormal, on-abort for narrower cases.

RestartSec=5 waits five seconds before restarting, which avoids hammering a failing dependency.

To prevent infinite loops, systemd limits attempts: StartLimitBurst=5 with StartLimitIntervalSec=60 means more than five starts in 60 seconds puts the unit into failed. Use systemctl reset-failed name afterwards.

A systemctl stop is intentional, so it is not restarted. Apply changes with daemon-reload then restart.

Example

bash
sudo systemctl edit myapp        # creates a drop-in override
# [Service]
# Restart=on-failure
# RestartSec=5
sudo systemctl restart myapp
sudo kill -9 $(systemctl show -p MainPID --value myapp)
systemctl is-active myapp
# active

What to say in the interview

"I set Restart=on-failure with a RestartSec delay in the unit, preferably via a systemctl edit drop-in. systemd also rate-limits restarts with StartLimitBurst, so a crash loop ends in failed instead of looping forever."

Common mistakes

  • Using Restart=always on a job that should exit cleanly, so it restarts endlessly.

#linux#systemd#reliability

Related questions

Practise more Linux & Shell questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Linux & Shell quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.