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

Take Your Linux Skills to the Next Level All courses, certifications, ad-free articles & community — from $8/mo
Join Root →
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
From $8/mo · or $59/yr billed annually · Cancel anytime

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.

Root Plan
Premium Linux Education for Serious Learners

Take Your Linux Skills to the Next Level

Root members get full access to every course, certification prep track, and a growing library of hands-on Linux content — with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Access to Linux certifications (RHCSA, RHCE, LFCS and LFCA)
Access new courses on release
Get access to weekly newsletter
Priority help in comments
Private Telegram community
Connect with the Linux community
Ravi Saive
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.

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.

Root Plan Premium Linux Education for Serious Learners

Before You Go - Upgrade Your Linux Skills

Root members get everything in one place, with new courses added every month.

What You Get
Ad-free access to all premium articles
Access to all courses: Learn Linux, AI for Linux, Bash Scripting, Ubuntu Handbook, Golang and more.
Linux certifications: RHCSA, RHCE, LFCS and LFCA
Access new courses on release
Weekly newsletter, priority support & Telegram community
Join Root Today and Start Learning Linux the Right Way
Structured courses, certification prep, and a community of Linux professionals - all in one membership.
Join Root Plan →
$8/mo · or $59/yr billed annually