Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Automatically Restart a Failed Service in Linux

Published

Linux and CommandsSystem Administration

Server downtime can cost your business money and hurt user trust. When critical services crash unexpectedly, every minute of downtime matters. Manual monitoring is not always possible, especially during nights and weekends. Setting up automatic service restart helps keep your systems running without constant human oversight. This guide shows you simple and effective ways to automatically restart failed services on Linux systems. You will learn multiple methods from basic systemd configuration to advanced monitoring tools. These techniques work on most Linux distributions and require only basic system administration skills.

Why Auto-Restart Services

Services can stop working for many reasons. When this happens, your website or application stops working too. Auto-restart helps fix this problem without you having to do anything.

Method 1: Using Systemd

Most Linux systems use systemd to manage services. You can tell systemd to restart services automatically.

Basic Setup

First, find your service file. It is usually in /etc/systemd/system/. Open the file with a text editor and add these lines:

sudo nano /etc/systemd/system/your-service-name.service

Or use vi editor:

sudo vi /etc/systemd/system/your-service-name.service

Add these lines in the [Service] section:

[Service]
Restart=always
RestartSec=10

The Restart=always line tells systemd to always restart the service. The RestartSec=10 line waits 10 seconds before restarting.

Save Changes

After you change the file, run these commands:

sudo systemctl daemon-reload
sudo systemctl restart your-service-name

Replace “your-service-name” with your actual service name.

Better Options

You can also use these settings:

[Service]
Restart=on-failure
RestartSec=5
StartLimitBurst=3
StartLimitInterval=60

This only restarts when the service fails. It tries 3 times in 60 seconds, then stops trying.

Method 2: Simple Check Script

You can write a simple script to check if your service is running. If not, it starts the service again.

Create the Script

Make a new file called check-service.sh using nano editor:

sudo nano check-service.sh

Then add this content:

#!/bin/bash


SERVICE="nginx"


if ! systemctl is-active --quiet $SERVICE; then
    echo "Service is down. Starting it now."
    systemctl start $SERVICE
fi

Change “nginx” to your service name.

Make it Run

Make the script executable:

chmod +x check-service.sh

Test the Script

Stop your service and run the script:

sudo systemctl stop nginx
./check-service.sh

The script should start your service again.

Method 3: Using Cron

Cron can run your check script automatically at set times.

Setup Cron

Open cron settings:

sudo crontab -e

Add this line to check every 5 minutes:

*/5 * * * * /path/to/check-service.sh

This runs your script every 5 minutes. If the service is down, it will start it again.

Check Every Minute

To check more often, use this:

* * * * * /path/to/check-service.sh

Method 4: Better Health Checks

Sometimes a service looks like it is running but does not work properly. You can check if the service really works.

Port Check Script

This script checks if your web service responds:

#!/bin/bash


SERVICE="nginx"
PORT="80"


if ! nc -z localhost $PORT; then
    echo "Port $PORT not working. Restarting service."
    systemctl restart $SERVICE
fi

This checks if port 80 responds. If not, it restarts nginx.

Install netcat

You may need to install netcat first:

sudo apt install netcat

Method 5: Log Everything

Always write down when services restart. This helps you find problems.

Add Logging

Change your script to save information. Edit the script file:

sudo nano check-service.sh

Replace the content with this:

#!/bin/bash


SERVICE="nginx"
LOG_FILE="/var/log/service-check.log"


if ! systemctl is-active --quiet $SERVICE; then
    echo "$(date): $SERVICE was down. Restarting now." >> $LOG_FILE
    systemctl start $SERVICE
    
    if systemctl is-active --quiet $SERVICE; then
        echo "$(date): $SERVICE started successfully." >> $LOG_FILE
    else
        echo "$(date): Failed to start $SERVICE." >> $LOG_FILE
    fi
fi

Check Logs

You can read the log file to see what happened:

tail -f /var/log/service-check.log

Method 6: Using Monit Tool

Monit is a special program that watches services and restarts them automatically.

Install Monit

On Ubuntu:

sudo apt install monit

On CentOS:

sudo yum install monit

Setup Monit

Edit the monit config file using nano:

sudo nano /etc/monit/monitrc

Add these lines:

check process nginx with pidfile /var/run/nginx.pid
    start program = "/bin/systemctl start nginx"
    stop program = "/bin/systemctl stop nginx"
    if failed port 80 then restart

Start Monit

Enable and start monit:

sudo systemctl enable monit
sudo systemctl start monit

Monit will now watch nginx and restart it if port 80 stops working.

Best Tips

Set Limits: Do not let services restart forever. Set limits:

StartLimitBurst=5
StartLimitInterval=300

This allows 5 restarts in 5 minutes, then stops trying.

Test Your Setup

Always test by stopping your service:

sudo systemctl stop your-service

Then check if it starts again automatically.

Keep Logs Small

Log files can get very big. Clean them regularly:

sudo logrotate /etc/logrotate.conf

Check System Resources

Make sure restarts do not use too much CPU or memory. Monitor your system when services restart.

Common Problems

Service Does Not Restart

Check if your config is correct:

sudo systemctl status your-service

Look for error messages.

Too Many Restarts

If a service keeps failing, find out why. Check the service logs:

sudo journalctl -u your-service

Permission Errors

Make sure your scripts can restart services. You may need to run them as root or use sudo.

Security Notes

  • Keep your restart scripts safe:
  • Only give needed permissions
  • Protect log files
  • Check your configs regularly
  • Use normal user accounts when possible

Simple Monitoring

You can get alerts when services restart:

# Send simple email
echo "Service restarted at $(date)" | mail -s "Alert" [email protected]

Which Method to Use

For most people:

  • Use systemd if you just want basic restart
  • Use cron with scripts for more control
  • Use monit for advanced monitoring

Start simple with systemd. If you need more features, try the other methods.

Conclusion

Auto-restarting services keeps your system running smoothly. The easiest way is to use systemd restart options. For more control, write simple check scripts and run them with cron. Always test your setup and keep logs to track what happens.

Remember to set limits so services do not restart forever. Fix the real problems that cause services to fail instead of just restarting them all the time.

Related guides

Account services are unavailable until the production backend is configured.