监控MYSQL进程的脚本,故障则重启,如无法启动则Email通知

在这里贡献一本人写的一监控MYSQL进程的脚本,希望对大家有所帮助。

功能:

监控MYSQL进程,故障则重启,如无法启动则Email通知

代码如下:

#!/bin/bash  
#/usr/bin/nmap localhost | grep 3306  
#lsof -i:3306  
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`  
  
function checkMysqlStatus(){  
  /usr/bin/mysql -uroot -p11111 --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, please try to restart mysql by manual!" > /var/log/mysqlerr  
      #mail -s "WARN! server: $MYSQLIP  mysql is down" guanli@tiejiang.org < /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 $2}' | xargs kill -9  
  service mysql start  
}  
  
if [ "$MYSQLPORT" == "3306" ]  
then  
  checkMysqlStatus  
else  
  restartMysqlService  
fi

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/54824.html

(0)
上一篇 2021年8月7日
下一篇 2021年8月7日

相关推荐

发表回复

登录后才能评论