apache 和 nginx 切换
背景:web服务器上默认跑apache,但是偶尔会有负载超级高的时候,(可能是配置了华盾),idc机房又要求必须使用华盾,而且华盾不支持nginx。我们的服务器跑nginx两年一直好好的,可换apache后总是负载很高。所以我想到,当负载高到一定程度时,就切换nginx,当负载降下来再切换回apache。#! /bin/bash
killphp(){
if `pgrep 'php-fpm'>/dev/null` ; then
/usr/local/php/sbin/php-fpm stop
killall -9 php-fpm
fi
}
killnginx(){
if `pgrep 'nginx' >/dev/null`; then
/etc/init.d/nginx stop
killall -9 nginx
fi
}
killhttpd(){
if `pgrep 'httpd'>/dev/null`; then
killall httpd
kill -9 `ps aux |grep httpd |grep -v 'grep ' |awk '{print $2}'`
fi
}
starthttpd(){
if `pgrep 'httpd'>/dev/null`; then
echo ok >/dev/null
else
/usr/local/apache2/bin/apachectl start
fi
}
startibsys(){
if `pgrep 'ibsysServer'>/dev/null`; then
echo ok >/dev/null
else
/usr/local/ibsys/ibsysServer
fi
}
Load(){
w|grep load |awk -F 'average:' '{print $2}'|awk '{print $1}' |cut -d'.' -f 1
}
rmail="[email protected]"
L="35"
while :; do
killphp
killnginx
starthttpd
startibsys
load=`Load`
if [ $load -gt $L ]; then
free >>/tmp/load.log
top -bn1 |head -n 100 >>/tmp/load.log
killhttpd
killall -9 ibsysServer
sleep 30
killphp
killnginx
/usr/local/php/sbin/php-fpm start 2>/tmp/mail.txt
killhttpd
/etc/init.d/nginx start 2>>/tmp/mail.txt
echo "web-rr load is more than 50 and the apache stop." >>/tmp/mail.txt && mail -s 'web-rr load' $rmail </tmp/mail.txt
sleep 120
load2=`Load`
if [ $load2 -lt 15 ]; then
killphp
killnginx
starthttpd
startibsys
fi
fi
sleep 3
done 把这个脚本丢到后台运行即可。
killphp(){
if `pgrep 'php-fpm'>/dev/null` ; then
/usr/local/php/sbin/php-fpm stop
killall -9 php-fpm
fi
}
killnginx(){
if `pgrep 'nginx' >/dev/null`; then
/etc/init.d/nginx stop
killall -9 nginx
fi
}
killhttpd(){
if `pgrep 'httpd'>/dev/null`; then
killall httpd
kill -9 `ps aux |grep httpd |grep -v 'grep ' |awk '{print $2}'`
fi
}
starthttpd(){
if `pgrep 'httpd'>/dev/null`; then
echo ok >/dev/null
else
/usr/local/apache2/bin/apachectl start
fi
}
startibsys(){
if `pgrep 'ibsysServer'>/dev/null`; then
echo ok >/dev/null
else
/usr/local/ibsys/ibsysServer
fi
}
Load(){
w|grep load |awk -F 'average:' '{print $2}'|awk '{print $1}' |cut -d'.' -f 1
}
rmail="[email protected]"
L="35"
while :; do
killphp
killnginx
starthttpd
startibsys
load=`Load`
if [ $load -gt $L ]; then
free >>/tmp/load.log
top -bn1 |head -n 100 >>/tmp/load.log
killhttpd
killall -9 ibsysServer
sleep 30
killphp
killnginx
/usr/local/php/sbin/php-fpm start 2>/tmp/mail.txt
killhttpd
/etc/init.d/nginx start 2>>/tmp/mail.txt
echo "web-rr load is more than 50 and the apache stop." >>/tmp/mail.txt && mail -s 'web-rr load' $rmail </tmp/mail.txt
sleep 120
load2=`Load`
if [ $load2 -lt 15 ]; then
killphp
killnginx
starthttpd
startibsys
fi
fi
sleep 3
done
none