Auto Restart When MySQL MariaDB Down

Few times ago there was one of our clients had error problem that the database server had been always down. We already suggested to upgrade the memory but our clients didn't agree with that. Finally, we take conclusion to make bash script for this kind of issue.

here is a simple shell script tested on CentOS / RHEL / Fedora / Debian / Ubuntu Linux. this script work it under any other UNIX liker operating system. It will check for httpd pid using pgrep command

pgrep command


pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to screen. If no process found it will simply return exit status 0 (zero). Below is the script:


#monitor db
#!/bin/bash
# MariaDB Process Monitor
# Restart MariaDB When It Goes Down
# -------------------------------------------------------------------------
# RHEL / CentOS / Fedora Linux restart command
RESTART="/sbin/service mariadb restart"

# uncomment if you are using Debian / Ubuntu Linux
#RESTART="/etc/init.d/mariadb restart"

#path to pgrep command
PGREP="/usr/bin/pgrep"

# daemon name
database="mysql"

# find database pid
$PGREP ${database}

if [ $? -ne 0 ] # if mariadb not running
then
 # restart mariadb
 $RESTART
fi


save the script with name maria.sh and then edit your crontab file

crontab -e


add this line

2 * * * * /bin/bash /root/maria.sh >/dev/null 2>&1


Good luck!
Previous
Next Post »