How to Automatically Restart a Linux Service After a Crash
This article was last updated on: May 17, 2026 am
Overview
Recently I encountered a Linux Systemd service crash that required manual intervention to restart. So, is there a way to automatically restart a Linux service after a crash?
Systemd
Systemd Restart
Systemd allows you to configure services to automatically restart when they crash.
A typical unit file looks like this:
1 | |
In the example above, if the daemon crashes or gets killed, systemd won’t do anything about it.
However, you can have systemd automatically restart the daemon in case it crashes or gets killed unexpectedly. To do this, add the Restart option to the [Service] section. A typical example looks like this:
1 | |
This will react to any situation that causes the daemon to stop — whenever the daemon stops, systemd will restart it within 5 seconds.
Restart has 2 commonly used values:
- always
- on-failure: Restart on failure. This covers the broadest range of failure scenarios, such as unclean signals and unclean exit codes.
In this example, the [Unit] section also includes the StartLimitIntervalSec and StartLimitBurst directives. This prevents a failing service from being restarted every 5 seconds indefinitely. If it keeps failing, systemd will stop trying to start the service.
If the service fails to restart 5 times within 600 seconds, it will enter a failed state and no further restart attempts will be made. This ensures that if the service is truly broken, systemd won’t keep trying to restart it. At that point, manual intervention is needed.
If you query the status of the daemon after it has been killed, systemd will show it as activating (auto-restart).
Systemd OnFailure
Restarting a service is great, but taking specific actions when a unit fails is even better. Perhaps the software you’re using has a known bug that requires deleting cache files on crash, or maybe you want to launch a script to collect logs and system information for diagnosing the issue. Systemd allows you to specify a unit to run when a service fails.
1 | |
This example specifies OnFailure=k3s-recovery.service to tell systemd that if the service fails, it should start the k3s-recovery unit.
The k3s-recovery unit is simply a oneshot service unit that runs a script:
1 | |
This script can do anything: perform some manual workarounds to get the service running again, send alerts to a monitoring system, or compress temporary logs and application state for troubleshooting. Here’s an example:
1 | |
Systemd FailureAction Reboot
There’s another possibility — a reboot cures everything! Systemd has a built-in feature to trigger a system reboot when a unit fails. In this example, the system will gracefully reboot when the unit enters a failure state:
1 | |
FailureAction accepts several valid values: none, reboot, reboot-force, reboot-immediate, poweroff, poweroff-force, poweroff-immediate, exit, exit-force, soft-reboot, soft-reboot-force, kexec, kexec-force, halt, halt-force, and halt-immediate.
Summary
This article covered several approaches for automatically handling failures when a service crashes. Systemd includes powerful features that can automatically respond to keep services running.