blog posts

负载高,自动重启mysql,apache

#!/bin/sh
# crontab: */3 * * * * /root/loadavg_restart_apache.sh >> /var/log/restart_apache.log
sync
sync
echo 3 > /proc/sys/vm/drop_caches
TOP_SYS_LOAD_NUM=5
SYS_LOAD_NUM=`uptime | awk ‘{print $(NF-2)}’ | sed ‘s/,//’`
echo $(date +”%y-%m-%d”) `uptime`
if [ `echo “$TOP_SYS_LOAD_NUM < $SYS_LOAD_NUM”|bc` -eq 1 ]
then
sync
echo “#0#” $(date +”%y-%m-%d %H:%M:%S”) “pkill httpd” `ps -ef | grep httpd | wc -l`
echo “#0#” $(date +”%y-%m-%d %H:%M:%S”) “pkill mysql” `ps -ef | grep mysql | wc -l`
service httpd stop
service mysql stop
sleep 8
pkill httpd
pkill mysql

sleep 8
for i in 1 2 3
do
if [ `pgrep mysql | wc -l` -le 0 ]
then
/sbin/service mysql start

sleep 30
echo “#1#” $(date +”%y-%m-%d %H:%M:%S”) “start mysql” `ps -ef | grep mysql | wc -l`

fi

if [ `pgrep httpd | wc -l` -le 0 ]
then

service httpd start

      service wdapache start

sleep 30

echo “#1#” $(date +”%y-%m-%d %H:%M:%S”) “start httpd” `ps -ef | grep httpd | wc -l`
fi

done
else
if [ `pgrep mysql | wc -l` -le 0 ]
then
/sbin/service mysql start

sleep 30
echo “#2#” $(date +”%y-%m-%d %H:%M:%S”) “start mysql” `ps -ef | grep mysql | wc -l`

fi

if [ `pgrep httpd | wc -l` -le 0 ]
then

service httpd start

 

sleep 30

echo “#2#” $(date +”%y-%m-%d %H:%M:%S”) “start httpd” `ps -ef | grep httpd | wc -l`
fi

fi

将以上文件,保存在 /root/loadavg_restart_apa_my.sh

然后在

crontab -e

添加

*/3 * * * * /root/loadavg_restart_apa_my.sh >> /var/log/restart_apa_my.log

*/3 ***** 表示3分钟,/root/loadavg_restart_apa_my.sh 你可以自定义。

Leave a Reply