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.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

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.