RackNerd Billboard Banner

How to Automatically Restart a Failed Service in Linux

When a critical service fails on your Linux server, you don’t want to wait around to restart it manually. Whether it’s your web server, database, or custom daemon, downtime means lost productivity—or worse.

Luckily, systemd makes it easy to automatically restart failed services. Here’s how to set it up, step by step.


Step 1: Check if Your Service Uses systemd

Most modern Linux distributions—like Ubuntu (16.04+), CentOS 7+, Debian 8+, and Fedora—use systemd as the init system.

To check if your service is managed by systemd, run:

systemctl status your-service-name

If you get output showing Loaded: loaded and Active: active or failed, you’re good to go.


Step 2: Edit the Service Unit File

Service files are usually located in:

  • /etc/systemd/system/ (custom services)
  • /lib/systemd/system/ or /usr/lib/systemd/system/ (default system services)

Let’s say your service is called myapp.service. Open its unit file:

sudo nano /etc/systemd/system/myapp.service

If the file doesn’t exist there, look in /lib/systemd/system/.


Step 3: Add Restart Options

Under the [Service] section, add or modify these lines:

Restart=on-failure
RestartSec=5

Here’s what they do:

  • Restart=on-failure: Tells systemd to restart the service if it exits with an error (non-zero status).
  • RestartSec=5: Waits 5 seconds before attempting a restart. This can help avoid crash loops.

Optional: You can also add StartLimitInterval and StartLimitBurst to control how many restarts are allowed within a time window to avoid excessive failures.

Example:

StartLimitInterval=60
StartLimitBurst=3

This means: Allow 3 restarts per 60 seconds.


Step 4: Reload systemd and Restart the Service

After editing the file, run:

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl restart myapp.service

Use daemon-reexec if you’ve made deeper changes or you’re debugging; otherwise, daemon-reload is enough.


Step 5: Check the Configuration

Use this command to confirm your settings:

systemctl show myapp.service | grep -i restart

You should see something like:

Restart=on-failure
RestartSec=5s

Final Thoughts

That’s it. With just a few lines added to your service unit file, your Linux system can automatically restart services when they fail—no manual intervention, no panic, just uptime.

This is especially useful for production systems where reliability is non-negotiable. Whether you’re managing a single VPS or a fleet of servers, automating service recovery is a smart move.


Want to go further? You can add logging, email alerts, or integrate with monitoring tools like Prometheus or Nagios for more visibility into what’s happening when a service fails.

Let me know if you want a guide on that next.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

RackNerd Billboard Banner
Copy link