在/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