This site runs best with JavaScript enabled.

Keep Webmin Running


A simple bash script to check if services are running and start them if not.

I have a Webmin/Virtualmin server where I host a few personal sites and lately certain services have been crashing. I don't want to upgrade to a larger server as these sites are not really critical to be up 24x7, but it is annoying.

After doing some searching I have found a simple script to run to check certain services statuses to see if they are still running, and, if not, start them back up.

There may be a better way to do this without the copy/paste code, but I'm not a bash scripting expert so this fits my needs.

I created a file at /etc/services.chk with the following content:

1#!/bin/bash
2
3STATUS="$(systemctl is-active webmin)"
4if [ "${STATUS}" != "active" ]; then
5 echo "Webmin is stopped. Restarting..."
6 /usr/sbin/service webmin start
7else
8 echo "Webmin is running."
9fi
10
11STATUS="$(systemctl is-active apache2)"
12if [ "${STATUS}" != "active" ]; then
13 echo "Apache is stopped. Restarting..."
14 /usr/sbin/service apache2 start
15else
16 echo "Apache is running."
17fi
18
19STATUS="$(systemctl is-active mysql)"
20if [ "${STATUS}" != "active" ]; then
21 echo "MySQL is stopped. Restarting..."
22 /usr/sbin/service mysql start
23else
24 echo "MySql is running."
25fi

I gave this script executable permissions:

1sudo chmod +x /etc/services.chk

I then did a bit of testing...

1$ sudo ./services.chk
2Webmin is running.
3Apache is running.
4MySql is running.
5$ sudo service webmin stop
6$ sudo ./services.chk
7Webmin is stopped. Restarting...
8Apache is running.
9MySql is running.

Finally, I added the following to my cron:

1*/30 * * * * /etc/services.chk >/dev/null 2>&1

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter



Dustin Davis