全自动监控Mysql服务脚本-定位原创
第一种:(主要通过awk命令过滤3306端口号)#/bin/bash
#owner:created by dingwei
#time:2016-11-04
#mail:1074189538@qq.com
#func:mysql service is started
#定义一个变量用来接收端口号
PORT=`netstat -lnt | grep 3306 | awk -F '[ :]+' '{print $5}'`
#判断该变量是否是3306端口号
if [ "$PORT" == "3306" ]
then
echo "mysql service has started"
else
echo `service mysql start`
fi
第二种:(主要通过wc命令过滤)
#/bin/bash
#owner:created by dingwei
#time:2016-11-14
#mail:1074189538@qq.com
#func:mysql service is started
#version:2.0
#定义一个变量用来接收端口号
portNUM=`netstat -lnt | grep 3306 | wc -l`
#判断该变量是否是3306端口号
if [ $portNUM -eq 1 ]
then
echo "mysql service has started"
else
echo `service mysql start`
fi
第三种:比较精确(主要通过判断端口号3306和进程号mysqld是否都是存在)
#/bin/bash
#owner:created by dingwei
#time:2016-11-14
#mail:1074189538@qq.com
#func:mysql service is started
#version:3.0
#只有在端口号和进程号都存在的情况下,才算mysql服务正常启动
#定义一个变量用来接收端口号
portNUM=`netstat -lnt | grep 3306 | wc -l`
mysqlProcessNum=`ps -ef | grep mysqld | grep -v grep |wc -l`
#判断该变量是否是3306端口号
if [ $portNUM -eq 1 -a $mysqlProcessNum -eq 2 ];then
echo "mysql service has started"
else
/etc/init.d/mysql start
fi
第四种:最精确完美(在第三种的基础上,为了防止mysql服务无法启动,进行了一些条件判断)
#/bin/bash
#owner:created by dingwei
#time:2016-11-14 /etc/init.d/mysql
#mail:1074189538@qq.com
#func:mysql service is started
#version:4.0
#只有在端口号和进程号都存在的情况下,才算mysql服务正常启动,为了防止启动mysql服务
#失败,给予相应的处理
#定义一个变量用来接收端口号
MYSQL=/etc/init.d/mysql
LogPath=/tmp/mysql.log
portNUM=`netstat -lnt | grep 3306 | wc -l`
mysqlProcessNum=`ps -ef | grep mysqld | grep -v grep |wc -l`
#判断该变量是否是3306端口号
if [ $portNUM -eq 1 -a $mysqlProcessNum -eq 2 ];then
echo "mysql service has started"
else
$MYSQL start >$LogPath
sleep 10
mysqlProcessNum=`ps -ef | grep mysqld | grep -v grep |wc -l`
if [ $portNUM -ne 1 -a $mysqlProcessNum -ne 2 ];then
while true
do
killall mysqld >/dev/null 2>&1
[ $? -ne 0 ] && break
done
$MYSQL start >>$LogPath && status="successfully" || status="failure"
mail -s "mysql startup status is $status" 1074189538@qq.com <$LogPath
fi
fi
页:
[1]