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.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

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.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
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.

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.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.