Shell脚本范例(十二)——MySQL服务异常监测

本文分享常用的一些Shell脚本的例子,这是第十二篇——MySQL服务异常监测。

要求:用if条件语句对MySQL数据库服务是否正常进行检测,如果服务未启动,则启动相应的服务。

准备:

#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

yum -y install mysql-server

/etc/init.d/mysqld start

参考:

#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

yum -y install mysql-server

/etc/init.d/mysqld start
Olivers-MacBook:Shell-Examples oliver$ cat 12-1.sh 
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

if [`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'` -eq 3306 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
fi
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

if [ `lsof -i tcp:3306|wc -l` -gt 0 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

[ `rpm -qa nmap|wc -l` -lt 1 ] && yun -y install nmap &> /dev/null

if [ `nmap 127.0.0.1 -p 3306 2> /dev/null|grep open|wc -l` -gt 0 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

[ `rpm -qa nc|wc -l` -lt 1 ] && yum -y install nc &> /dev/null

if [ `nc -w 2 127.0.0.1 3306 &> /dev/null && echo ok|grep ok|wc -l` -gt 0 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

if [ `ps -ef|grep -v grep|grep mysql|wc -l` -gt 0 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi

标签: none