Keep Webmin Running
Photo by Andrea Leopardi on Unsplash
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/bash23STATUS="$(systemctl is-active webmin)"4if [ "${STATUS}" != "active" ]; then5 echo "Webmin is stopped. Restarting..."6 /usr/sbin/service webmin start7else8 echo "Webmin is running."9fi1011STATUS="$(systemctl is-active apache2)"12if [ "${STATUS}" != "active" ]; then13 echo "Apache is stopped. Restarting..."14 /usr/sbin/service apache2 start15else16 echo "Apache is running."17fi1819STATUS="$(systemctl is-active mysql)"20if [ "${STATUS}" != "active" ]; then21 echo "MySQL is stopped. Restarting..."22 /usr/sbin/service mysql start23else24 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.chk2Webmin is running.3Apache is running.4MySql is running.5$ sudo service webmin stop6$ sudo ./services.chk7Webmin 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