Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 100System · systemd

How to restart a service automatically when it crashes

An app crashes at 3 a.m. and stays down until someone wakes up. systemd can restart it in seconds — but done wrong, a broken app restarts forever and hides the real problem. This how-to adds a restart policy with a drop-in file, limits crash loops, sends an alert when it gives up, and tests it by killing the process.

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

100.1

The quick answer

systemctl edit shop-api
/etc/systemd/system/shop-api.service.d/override.conf
[Unit]
StartLimitIntervalSec=300
StartLimitBurst=5
 
[Service]
Restart=on-failure
RestartSec=5s
systemctl daemon-reload
systemctl restart shop-api

Restart=on-failure = restart after a crash, a non-zero exit, a signal or a timeout — but not after a clean systemctl stop.

100.2

Step by step

  1. Use a drop-in (systemctl edit) instead of changing the vendor's unit file — updates do not overwrite it.
  2. Choose the policy:
SettingRestarts when
Restart=on-failureCrash, exit code ≠ 0, killed by a signal, timeout. The normal choice.
Restart=alwaysAlso after a clean exit (for apps that should never end).
Restart=on-abnormalSignal or timeout only (not exit codes).
  1. Stop crash loops: with StartLimitBurst=5 in StartLimitIntervalSec=300, systemd gives up after 5 starts in 5 minutes — the unit goes to failed, which you can see and alert on. RestartSec=5s gives dependencies (database, network) time to come back.
  2. Alert when it gives up — OnFailure= starts another unit:
/etc/systemd/system/shop-api.service.d/override.conf (part)
[Unit]
OnFailure=notify-failure@%n.service

notify-failure@.service is your small unit that sends a mail or a webhook (with %i = the failed unit's name).

  1. Hangs are not crashes. If the app can freeze while the process stays alive, use a health check: a WatchdogSec= (the app must send keep-alives with sd_notify), or an external check (Prometheus, Uptime Kuma) that alerts.
  2. Test it — kill the main process and watch systemd bring it back:
systemctl show -p MainPID,NRestarts shop-api
MainPID=4211
NRestarts=0
kill -9 4211
sleep 6; systemctl show -p MainPID,NRestarts shop-api
MainPID=4388
NRestarts=1
  1. Containers: Docker --restart unless-stopped (or Compose restart:), Podman Quadlet Restart= in the unit, Kubernetes restarts containers by itself (restartPolicy).

100.3

Check that it worked

After kill -9, the service is active (running) again with a new PID and NRestarts went up; the journal shows why it died:

journalctl -u shop-api -n 20 --no-pager

100.4

Common problems

ProblemCauseFix
Start request repeated too quicklyThe start limit was hit (crash loop).Fix the real error (journal); systemctl reset-failed shop-api, then start.
StartLimit… settings ignoredPut in [Service] instead of [Unit].They belong in [Unit].
No restart after systemctl stopCorrect — a manual stop is not a failure.Start it again yourself.
Restarted, but the app is broken again at onceBad config, missing DB, full disk.Read the journal; restarting does not fix causes.
Override has no effectNo daemon-reload, or a typo in the section name.systemctl cat shop-api shows all parts; daemon-reload.
The app hangs, systemd thinks all is fineThe process is alive.Watchdog or an external health check with an alert.

100.5

Go further

1What is the difference between Restart=on-failure and Restart=always?

on-failure restarts after crashes and errors only; always also after a clean exit.

2Why set StartLimitBurst and StartLimitIntervalSec?

So a broken service does not restart forever; it goes to failed, which you can see and alert on.

Alle commando's in dit hoofdstuk