#!/bin/bash
#/usr/bin/nmap localhost | grep 3306
#lsof-i:3306
mysqlport= ' Netstat-na|grep "LISTEN" |grep "3306" |awk-f[: ""]+ ' {print $} '
function Checkmysqlstatus () {
/usr/bin/mysql-uroot-paaaaaa--connect_timeout=5-e "show databases;" &>/dev/null 2>&1
if [$?-ne 0]
Then
Restartmysqlservice
if ["$MYSQLPORT" = = "3306"];then
echo "MySQL restart successful ..."
Else
echo "MySQL restart failure ..."
echo "Server: $MYSQLIP MySQL is down,!" >/var/log/mysqlerr
#mail-S "warn! Server: $MYSQLIP MySQL is down "[email protected] </var/log/mysqlerr
fi
Else
echo "MySQL is running ..."
fi
}
function Restartmysqlservice () {
echo "Try to restart the MySQL service ..."
/bin/ps aux |grep mysql |grep-v grep | awk ' {print $} ' | Xargs kill-9
service MySQL start
}
if ["$MYSQLPORT" = = "3306"]
Then
Checkmysqlstatus
Else
Restartmysqlservice
fi
This article is from the "Broken Saber" blog, please be sure to keep this source http://90sec.blog.51cto.com/7404127/1707140
Shell monitors MySQL Status