Systemd Restart Policies
Understand all systemd restart policies: no, on-success, on-failure, on-abnormal, on-watchdog, on-abort, and always. Learn which exit conditions trigger each policy.
Detailed Explanation
Choosing the Right Restart Policy
The Restart= directive controls when systemd automatically restarts a service. Each policy responds differently to various exit conditions.
Policy Comparison Table
| Exit Condition | no | on-success | on-failure | on-abnormal | on-watchdog | on-abort | always |
|---|---|---|---|---|---|---|---|
| Clean exit (code 0) | - | Restart | - | - | - | - | Restart |
| Non-zero exit | - | - | Restart | - | - | - | Restart |
| Signal (SIGKILL, etc.) | - | - | Restart | Restart | - | Restart | Restart |
| Timeout | - | - | Restart | Restart | - | - | Restart |
| Watchdog timeout | - | - | Restart | Restart | Restart | - | Restart |
| Core dump (abort) | - | - | Restart | Restart | - | Restart | Restart |
Recommended Policies by Service Type
Web Servers and APIs: Restart=on-failure
Restart=on-failure
RestartSec=5
Restarts on crashes but not on clean shutdown. This is the safe default for most services.
Critical Infrastructure: Restart=always
Restart=always
RestartSec=3
Always restarts regardless of exit reason. Use for services that must always be running (databases, message queues, monitoring agents).
Batch Jobs: Restart=no
Restart=no
The service runs once and stays stopped. Appropriate for oneshot tasks and migration scripts.
Services with Watchdog: Restart=on-watchdog
Restart=on-watchdog
WatchdogSec=30
Only restarts if the watchdog timer expires (service failed to send heartbeat). The service must call sd_notify("WATCHDOG=1") periodically.
RestartSec and Rate Limiting
RestartSec= adds a delay between restart attempts:
RestartSec=5 # Wait 5 seconds before restarting
Systemd also has built-in rate limiting. If a service restarts more than StartLimitBurst times within StartLimitIntervalSec, the service enters a failed state:
StartLimitIntervalSec=60
StartLimitBurst=5
This prevents a crash loop from consuming all system resources.
RestartPreventExitStatus
You can exclude specific exit codes from triggering a restart:
Restart=on-failure
RestartPreventExitStatus=1 6 SIGTERM
This restarts on failure except when the process exits with code 1, 6, or is killed by SIGTERM. Useful when certain exit codes indicate a configuration error that restarting won't fix.
Viewing Restart History
systemctl show myapp.service --property=NRestarts
journalctl -u myapp.service | grep "Started\|Stopped\|Failed"
Use Case
Choosing the optimal restart policy for different types of services: web servers, background workers, batch jobs, and monitoring agents — each with different reliability and recovery requirements.