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:
- How to Set Up Auto-Restart for Failed Services on OpenRC
- How to Find Running Services in Linux with Systemd Commands
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.