$ run as a normal user# run as root (or with sudo)
The quick answer
systemctl edit shop-api[Unit]
StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
Restart=on-failure
RestartSec=5sRestart=on-failure = restart after a crash, a non-zero exit, a signal or a timeout — but not after a clean systemctl stop.
Step by step
- Use a drop-in (
systemctl edit) instead of changing the vendor's unit file — updates do not overwrite it. - Choose the policy:
| Setting | Restarts when |
|---|---|
Restart=on-failure | Crash, exit code ≠ 0, killed by a signal, timeout. The normal choice. |
Restart=always | Also after a clean exit (for apps that should never end). |
Restart=on-abnormal | Signal or timeout only (not exit codes). |
- Stop crash loops: with
StartLimitBurst=5inStartLimitIntervalSec=300, systemd gives up after 5 starts in 5 minutes — the unit goes tofailed, which you can see and alert on.RestartSec=5sgives dependencies (database, network) time to come back. - Alert when it gives up —
OnFailure=starts another unit:
[Unit]
OnFailure=notify-failure@%n.servicenotify-failure@.service is your small unit that sends a mail or a webhook (with %i = the failed unit's name).
- 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 withsd_notify), or an external check (Prometheus, Uptime Kuma) that alerts. - 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- Containers: Docker
--restart unless-stopped(or Composerestart:), Podman QuadletRestart=in the unit, Kubernetes restarts containers by itself (restartPolicy).
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-pagerCommon problems
| Problem | Cause | Fix |
|---|---|---|
Start request repeated too quickly | The start limit was hit (crash loop). | Fix the real error (journal); systemctl reset-failed shop-api, then start. |
StartLimit… settings ignored | Put in [Service] instead of [Unit]. | They belong in [Unit]. |
No restart after systemctl stop | Correct — a manual stop is not a failure. | Start it again yourself. |
| Restarted, but the app is broken again at once | Bad config, missing DB, full disk. | Read the journal; restarting does not fix causes. |
| Override has no effect | No 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 fine | The process is alive. | Watchdog or an external health check with an alert. |
Go further
- Command pages: systemctl, journalctl, kill.
- Next how-tos: Create a systemd service for your app, Kill a process that hangs, Send alerts to Slack or email.
- Learn it: RHCSA — Edit units, RHCSA — Your own service, RH124 — Troubleshoot services.
- Practise: Lab: manage and change services.
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.