MySQL monitoring script Linux. Monitor MySQL daemon.

MySQL monitoring script Linux.

Linux Bash script to monitor MySQL service. Requirements are the nmap port scanner but the script will install it on first run as long as you use yum package manager.

Create the file using nano.

nano -w

Add the following.


#Author Phil Bond Box Admin


#Check if nmap is installed otherwise install (ONLY TESTED WITH YUM)
if [ -e “/usr/bin/nmap” ] ; then
echo “nmap is installed” > /dev/null 2>&1
$packageman install nmap -y > /dev/null 2>&1


while [ $restartchk -gt 0 ]; do

portstatus=`nmap $server -p $port | tail -3 | grep $servicename | awk {‘print $2’}`

case $portstatus in
open)echo “Port is responding. $servicename is accepting connections” > /dev/null 2>&1 && let restartchk=0;;
closed)echo “Port is closed. $servicename is not responding” && echo “Attempting to restart Mysqld” && /sbin/service $service restart;;
filtered)echo “Port is filtered. $servicename is not responding” && /sbin/service $service restart;;

if [ $attempts -eq 3 ]; then
echo “Mysqld is failing to start after 3 attempts exiting”
exit 1

let attempts=attempts+1


exit 0

Press ctrl+x to exit you will be prompted to save.

Now add the script as a cronjob. Unless you changed your editor variable this will open in Vi.

crontab -e

Now press the insert key on your keyboard to enter edit mode and add the following.

*       0-23    *       *       *       /root/

Press the esc key on your keyboard to exit edit mode.

Press shift+:

Type wq and hit enter to save.

Now when ever MySQL is not responding you receive an email informing you and the script will attempt to restart the service but will quit if the service fails after 3 attempts. The destination email will be the email set for your cronjobs.

Bash Scripting, Linux, MySQL , ,

1 response to MySQL monitoring script Linux. Monitor MySQL daemon.

  1. box-admin

    Im thinking of replacing the port scans with sql queries. So nmap isnt required.

Leave a Reply

Your email address will not be published. Required fields are marked *

14 − three =