利用shell脚本监控MySQL进程

#!/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" sys@t667.com < /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

然后配置crontab任务即可
示例:

 */10 * * * * root /bin/sh /root/mysql.sh
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共1条
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容