在/etc/crontab里,定时执行检测脚本,检测系统的Apache,和Mysql进程是否超出标准,超标的话,自动重启,没有的话,记录进程数据.
Crontab的内容(每30分钟执行一次,并将结果记录在/var/log/chksys.log)
0,30 * * * * root /usr/local/chksys.sh >> /var/log/chksys.log
/usr/local/chksys.sh代码:
#!/bin/bash
#check apache,mysql thread and auto reboot system
#Powered by ipaddr(aspbiz)
#config
MaxApacheThread=300
MaxMysqlThread=250
NeedReboot=0
ApacheThread=`ps -A|grep http|wc -l`
MysqlThread=`ps -A|grep mysql|wc -l`
if [ $ApacheThread -gt $MaxApacheThread ]
then
NeedReboot=1
fi
if [ $MysqlThread -gt $MaxMysqlThread ]
then
NeedReboot=1
fi
if [ $NeedReboot -eq 1 ]
then
echo "-----------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "-----------------------------"
echo "Apache:$ApacheThread;Mysql:$MysqlThread."
echo "System is busy,reboot"
reboot
else
echo "-----------------------------"
echo $(date +"%y-%m-%d %H:%M:%S")
echo "-----------------------------"
echo "Apache:$ApacheThread;Mysql:$MysqlThread."
echo "System is normal"
fi