Automatically check MariaDB to ensure it is running

I’ve noticed on a couple occasions (on different servers) that MariaDB will “die”. On the first server, this was related to the server running out of memory. I’m not sure about the other server I noticed this on yet. In any case, I simply wrote a small bash script to check the status of MariaDB every ten minutes, and restart it if it’s down. Here’s what the script looks like…

/root/cronscripts/check-db-server.sh:

#!/bin/bash
echo "Checking whether MariaDB is running or not."
if [ $(/bin/systemctl status mariadb.service | grep -c "active (running)") -gt 0 ]; then
 echo "MariaDB is running. "
else
 echo "MariaDB is not running. Starting MariaDB service."
 /bin/systemctl restart mariadb.service
fi

Then I just call the script every ten minutes in a cronjob like this…

*/10 * * * * /root/cronscripts/check-db-server.sh > /dev/null

 

Leave a Comment