How to Set Up Auto-Restart for Failed Services on OpenRC

OpenRC is a fast and lightweight init system used by many Linux distributions like Alpine, Gentoo, and Artix. It helps manage services, ensuring they start, stop, and restart correctly.

However, if a service crashes or stops unexpectedly, it won’t restart automatically, to fix such an issue, you need to set up a system to restart services automatically after a failure.

In this guide, we’ll show you how to configure OpenRC to monitor and restart services automatically when they fail.

Step 1: Check Service Status in OpenRC

Before setting up auto-restart, check if the service is running properly.

rc-service nginx status

To see all active services.

rc-status

To make sure the service starts when the system boots, add it to the default runlevel.

rc-update add nginx default

To confirm that the service is added.

rc-update show | grep nginx

Step 2: Create a Service Monitor Script

To automatically restart a service if it stops, create a monitoring script that checks the service and restarts it if necessary.

sudo nano /usr/local/bin/service-monitor.sh

Add the following content to the file.

#!/bin/bash

SERVICE="<service-name>"

if ! rc-service $SERVICE status | grep -q "started"; then
  echo "$(date): $SERVICE is down. Restarting..." >> /var/log/service-monitor.log
  rc-service $SERVICE restart
fi

Save the file and make the script executable.

sudo chmod +x /usr/local/bin/service-monitor.sh

Step 3: Set Up a Cron Job to Monitor the Service

Now that the monitoring script is ready, set up a cron job to run it regularly.

crontab -e

Add this line to run the script every 5 minutes.

*/5 * * * * /usr/local/bin/service-monitor.sh

Save and exit the editor.

Step 4: Test the Configuration

To test whether the service restarts correctly, you need to stop the service manually.

rc-service nginx stop

Wait for 5 minutes and check if the service is restarted.

rc-service nginx status

Check the log to confirm that the service was restarted.

cat /var/log/service-monitor.log

Bonus: Use Monit for Advanced Monitoring

For more advanced monitoring and automatic restarts, you can use tools like Monit, which allows you to monitor multiple services and automatically restart them if they crash.

To install Monit on your system:

sudo apt install monit   # For Debian/Ubuntu
sudo apk add monit       # For Alpine Linux
sudo emerge --ask monit  # For Gentoo

To enable Monit at system startup and start the service.

rc-update add monit default
rc-service monit start

To check the status.

rc-service monit status

To monitor a service, you need to create a monit configuration file.

sudo nano /etc/monitrc

Add the following lines at the end of the file to monitor a service (replace <service-name> with the actual service name):

check process <service-name> with pidfile /run/<service-name>.pid
    start program = "/etc/init.d/<service-name> start"
    stop program = "/etc/init.d/<service-name> stop"
    if 3 restarts within 5 cycles then timeout

For example, to monitor nginx:

check process nginx with pidfile /run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop program = "/etc/init.d/nginx stop"
    if 3 restarts within 5 cycles then timeout

Save the file and reload the Monit configuration to apply the changes:

monit reload

Enable Monit Web Interface (Optional)

To enable the Monit web interface and manage services via a browser, you need to open a Monit configuration file:

sudo nano /etc/monitrc

Uncomment and edit the following lines.

set httpd port 2812
    use address 0.0.0.0    # Listen on all interfaces
    allow admin:monit      # Set username and password (change as needed)

Save and restart Monit.

rc-service monit restart

Access the Monit web interface.

http://your-server-ip:2812

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

These guides cover detailed steps for handling service failures on different Linux systems.

Conclusion

By following these steps, you can ensure that your critical services running on OpenRC restart automatically after any failure. This setup reduces downtime and keeps your system running smoothly.

Whether you use a simple script or a more advanced monitoring tool like Monit, keeping services running is essential for system stability.

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.