定时监控服务器负载,超过一定阀值后,自动重启web服务。
apache安装目录:/usr/local/apache
脚本运行日志文件:/usr/local/apache/check_cpu_load.log
脚本存放目录:/home/crontab
cd /home/crontab
vi check_cpu_load.sh #编辑,添加以下代码
#!/bin/sh
#crontab: */5 * * * * /home/crontab/check_cpu_load.sh >> /usr/local/apache/check_cpu_load.log
TOP_SYS_LOAD_NUM=3
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/local/apache/check_cpu_load.log
/usr/local/apache/bin/apachectl stop
sleep 5
pkill httpd
for i in 1 2 3
do
if [ `pgrep httpd | wc -l` -le 0 ]
/usr/local/apache/bin/apachectl start
sleep 30
echo "#1#" $(date +"%y-%m-%d %H:%M:%S") "start httpd" `ps -ef | grep httpd | wc -l` >> /usr/local/apache/check_cpu_load.log
fi
done
else
echo "#2#" $(date +"%y-%m-%d %H:%M:%S") "start httpd" `ps -ef | grep httpd | wc -l` >> /usr/local/apache/check_cpu_load.log
:wq! #保存退出
crontab -e #添加计划任务
*/5 * * * * /home/crontab/check_cpu_load.sh >> /usr/local/apache/check_cpu_load.log
service crond restart #重启计划任务
通过以上设置后,当服务器负载超过3时,在5分钟内会自动运行脚本,重启apache服务,使网站恢复访问
本文出自http://www.111cn.net/sys/CentOS/102450.htm
原文链接:https://www.f2er.com/centos/379623.html