APACHE负载过高自动重启的办法

抽时间找了一个centos/linux负载过高自动重启apache的脚本,保护你的vps不因超负荷被关闭。稍微改了点,适合所有可以通过service httpd start | stop 来重启apache的vps或服务器。最后别忘记在crontab设置每隔几分钟执行一次即可。

一行命令自动安装,安装成功后请看 crontab -l :

wget http://www.ctohome.com/linux-vps-pack/restart_apache_by_load.sh;sh restart_apache_by_load.sh;

TOP_SYS_LOAD_NUM表示到达这个负载的时候,就会停止apache。然后过段时间负载降下来后,再尝试启动apache

#!/bin/sh

# crontab usage: */3 * * * * /backup/www.ctohome.com/loadavg_restart_apache.sh >> /backup/www.ctohome.com/restart_apache.log

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 echo "#0#" $(date +"%y-%m-%d %H:%M:%S") "pkill httpd" `ps -ef | grep httpd | wc -l` /usr/sbin/apachectl stop sleep 8 pkill httpd sleep 8 for i in 1 2 3 do if [ `pgrep httpd | wc -l` -le 0 ] then /usr/sbin/apachectl 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 httpd | wc -l` -le 0 ] then /usr/sbin/apachectl start sleep 30 echo "#2#" $(date +"%y-%m-%d %H:%M:%S") "start httpd" `ps -ef | grep httpd | wc -l` fi fi 没有什么坏处,至少还可以监控系统的历史负载。下面的是某个vps设置了最大负载3的重启apache情况,效果不错: tail -f restart_apache.log 10-07-17 13:57:01 up 14:53, 2 users, load average: 0.34, 1.09, 1.14 10-07-17 14:00:01 up 14:56, 2 users, load average: 0.97, 1.06, 1.11 10-07-17 14:03:01 up 14:59, 2 users, load average: 1.38, 1.26, 1.18 10-07-17 14:06:01 up 15:02, 2 users, load average: 0.63, 0.92, 1.06 10-07-17 14:09:01 up 15:05, 2 users, load average: 0.98, 0.81, 0.98 10-07-17 14:12:01 up 15:08, 2 users, load average: 0.55, 0.71, 0.90 10-07-17 14:15:01 up 15:11, 2 users, load average: 0.46, 0.59, 0.81 10-07-17 14:18:02 up 15:14, 2 users, load average: 3.06, 1.30, 1.01 #0# 10-07-17 14:18:02 pkill httpd 38 #1# 10-07-17 14:18:51 start httpd 34 10-07-17 14:21:01 up 15:17, 2 users, load average: 1.14, 1.59, 1.20 10-07-17 14:24:01 up 15:20, 2 users, load average: 1.19, 1.45, 1.21 10-07-17 14:27:01 up 15:23, 2 users, load average: 0.29, 0.95, 1.05 10-07-17 14:30:01 up 15:26, 2 users, load average: 0.99, 0.85, 0.98 10-07-17 14:33:01 up 15:29, 2 users, load average: 1.15, 1.22, 1.12 10-07-17 14:36:01 up 15:32, 2 users, load average: 4.58, 2.94, 1.78 #0# 10-07-17 14:36:01 pkill httpd 36 #1# 10-07-17 14:36:49 start httpd 27 10-07-17 14:39:01 up 15:35, 2 users, load average: 2.24, 2.49, 1.79 10-07-17 14:42:01 up 15:38, 2 users, load average: 1.37, 1.87, 1.66 10-07-17 14:45:01 up 15:41, 2 users, load average: 0.69, 1.44, 1.54 10-07-17 14:48:01 up 15:44, 2 users, load average: 1.73, 1.68, 1.62 10-07-17 14:51:01 up 15:47, 2 users, load average: 0.81, 1.34, 1.49 10-07-17 14:54:01 up 15:50, 2 users, load average: 1.71, 1.25, 1.41 10-07-17 14:57:01 up 15:53, 2 users, load average: 1.93, 1.52, 1.47 10-07-17 15:00:01 up 15:56, 2 users, load average: 1.29, 1.43, 1.44 10-07-17 15:03:01 up 15:59, 2 users, load average: 1.45, 1.42, 1.43 10-07-17 15:06:01 up 16:02, 2 users, load average: 0.73, 1.24, 1.36 10-07-17 15:09:01 up 16:05, 2 users, load average: 1.66, 1.55, 1.47 10-07-17 15:12:03 up 16:08, 2 users, load average: 3.59, 2.02, 1.63 #0# 10-07-17 15:12:07 pkill httpd 35 #1# 10-07-17 15:12:55 start httpd 28 10-07-17 15:15:01 up 16:11, 2 users, load average: 1.19, 1.78, 1.63 10-07-17 15:18:01 up 16:14, 2 users, load average: 1.38, 1.60, 1.58 10-07-17 15:21:01 up 16:17, 2 users, load average: 3.32, 2.20, 1.79 #0# 10-07-17 15:21:01 pkill httpd 33 #1# 10-07-17 15:21:50 start httpd 33 10-07-17 15:24:01 up 16:20, 2 users, load average: 1.18, 1.66, 1.64 10-07-17 15:27:01 up 16:23, 2 users, load average: 1.67, 1.73, 1.67 10-07-17 15:30:01 up 16:26, 2 users, load average: 1.15, 1.55, 1.60 10-07-17 15:33:01 up 16:29, 2 users, load average: 0.85, 1.30, 1.49 10-07-17 15:36:01 up 16:32, 2 users, load average: 0.30, 0.89, 1.30 10-07-17 15:39:01 up 16:35, 2 users, load average: 0.37, 0.90, 1.25 脚本执行如果报错 /backup/www.cthome.com/loadavg_restart_apache.sh: line 6: bc: command not found /backup/www.cthome.com/loadavg_restart_apache.sh: line 6: [: -eq: unary operator expected 表示没有bc命令,centos可以用 yum -y install bc 安装它

Leave a Reply

Your email address will not be published.