检测mysql进程并重启服务

服务器上的mysql老是拖累apache,导致WWW服务不正常,前面修改过对应的mysql参数,但还是不行,因此,就决定设定让系统CRON下面这个脚本,注意脚本来自傅翮鹤 ,我仅仅修改了ubuntu下对应的参数而已。

#!/bin/bash
#check apache,mysql thread and auto reboot service
#Powered by 傅翮鹤 [www.fuhehe.com]
#Date 2007-06-15

#config
MaxApacheThread=100
MaxMysqlThread=50
HttpService=`ls /etc/init.d|grep apache2`
MysqlService=`ls /etc/init.d|grep mysql`

ApacheThread=`ps -A|grep apache2|wc -l`
MysqlThread=`ps -A|grep mysql|wc -l`

NeedReboot=0

if [ $ApacheThread -gt $MaxApacheThread ]
then
NeedReboot=1
fi
if [ $ApacheThread -eq 0 ]
then
NeedReboot=2
fi

if [ $MysqlThread -gt $MaxMysqlThread ]
then
NeedReboot=3
fi
if [ $MysqlThread -eq 0 ]
then
NeedReboot=4
fi

if [ $NeedReboot -eq 1 ]
then
echo "-----------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "-----------------------------"
echo "Apache:$ApacheThread;Mysql:$MysqlThread."
echo "Apache is busy,reboot"
/etc/init.d/$HttpService stop
/etc/init.d/$HttpService start
elif [ $NeedReboot -eq 2 ]
then
echo "-----------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "-----------------------------"
echo "Apache:$ApacheThread;Mysql:$MysqlThread."
echo "Apache is down,reboot"
/etc/init.d/$HttpService start
elif [ $NeedReboot -eq 3 ]
then
echo "-----------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "-----------------------------"
echo "Apache:$ApacheThread;Mysql:$MysqlThread."
echo "Mysql is busy,reboot"
/etc/init.d/$MysqlService stop
/etc/init.d/$MysqlService start
elif [ $NeedReboot -eq 4 ]
then
echo "-----------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "-----------------------------"
echo "Apache:$ApacheThread;Mysql:$MysqlThread."
echo "Mysql is down,reboot"
/etc/init.d/$MysqlService start
else
echo "-----------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "-----------------------------"
echo "Apache:$ApacheThread;Mysql:$MysqlThread."
echo "System is normal"
fi

This entry was posted in Linux and tagged , , , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please copy the string Cb0hI6 to the field below:

以新浪微博帐号登录