How to Automatically Restart a Service After Failure on SysVinit and Upstart

On systemd-based Linux distributions, managing and restarting services automatically after a failure is relatively straightforward. However, many older or minimal Linux systems rely on alternative init systems such as SysVinit and Upstart, which require different approaches to manage and restart services.

In this guide, we’ll explore how to automatically restart a failed service on non-systemd systems using SysVinit and Upstart.

1. Restarting Services Automatically with SysVinit

SysVinit is one of the oldest init systems, commonly used in distributions like Debian and CentOS before the transition to systemd.

Step 1: Install and Configure monit

monit is a lightweight, open-source utility that monitors services and automatically restarts them when they fail.

# On Debian/Ubuntu
sudo apt update
sudo apt install monit

# On CentOS/RHEL
sudo yum install monit

Step 2: Configure Monit to Monitor a Service

Edit the Monit configuration file:

sudo nano /etc/monit/monitrc

Add a service definition:

# Example: Monitor Apache service
check process apache2 with pidfile /var/run/apache2/apache2.pid
    start program = "/etc/init.d/apache2 start"
    stop program = "/etc/init.d/apache2 stop"
    if failed port 80 protocol http then restart
    if 5 restarts within 5 cycles then timeout

Explanation of the above service definition:

  • check process apache2 – Defines the service to monitor.
  • start/stop program – Commands to start and stop the service.
  • if failed port 80 – Restarts if the HTTP port becomes unreachable.

Next, enable, start, and verify the status of monit.

sudo systemctl enable monit
sudo systemctl start monit
sudo monit status

2. Restarting Services Automatically with Upstart

Upstart was the default init system on Ubuntu before systemd, and it uses configuration files located in /etc/init/ to define service management.

Step 1: Create an Upstart Configuration File

Create a custom Upstart configuration for the service.

sudo nano /etc/init/apache2.conf

Add the following content.

# Apache service monitoring
description "Apache2 Service"
start on runlevel [2345]
stop on runlevel [!2345]

respawn
respawn limit 10 5

exec /usr/sbin/apache2ctl -D FOREGROUND

Explanation of the above configuration:

  • respawn – Automatically restart the service if it fails.
  • respawn limit 10 5 – Limits restarts to 10 attempts within 5 seconds to prevent excessive restarts.

Next, enable, start, and manage the service.

sudo start apache2
sudo stop apache2
sudo status apache2

To automatically enable the service on startup.

sudo update-rc.d apache2 defaults

3. Using Cron to Restart Services Manually

If monit or Upstart is unavailable, a fallback approach is to use a cron job to periodically check and restart the service.

Create a shell script.

sudo nano /usr/local/bin/check_apache.sh

Add the following content.

#!/bin/bash
if ! pgrep -x "apache2" > /dev/null
then
    /etc/init.d/apache2 start
fi

Make the script executable.

sudo chmod +x /usr/local/bin/check_apache.sh

Add a cron job to run the script.

sudo crontab -e

Add the following line to check the service every 5 minutes.

*/5 * * * * /usr/local/bin/check_apache.sh

If you’re interested in setting up auto-restart for other init systems, check out these articles:

Conclusion

Automatically restarting failed services on non-systemd systems requires a bit more manual setup, but tools like monit, Upstart, or cron scripts can efficiently handle service failures and keep your applications running smoothly.

If you’re still using a non-systemd system, it might be worth considering an upgrade to a systemd-based distribution for easier service management.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.