CentOS 监视 apache服务器状态并自动重启脚本

有时候我们想监视web服务器的运行状态,并在它出现问题比如down掉或者因为某种原因service unavailable的时候重启它。
写了这样一个简单的脚本:

#!/bin/bash
	
URL="http://localhost/"
TIMEOUT=20
MAXTIME=20
INTERVAL=180
LOG=httpd.log
HTTPD=httpd-itk

while true; do
	result=$(curl --connect-timeout ${TIMEOUT} --max-time ${MAXTIME} --head --silent ${URL} | grep '200')
	if [[ ! -z ${result} ]]; then
		sleep ${INTERVAL}
		continue
	fi

	top -n 1 -b >> ${LOG}
	/usr/bin/killall -9 httpd &> /dev/null
	/usr/bin/killall -9 php5-cgi &> /dev/null
	/usr/bin/killall -9 php-cgi &> /dev/null
	sleep 3
        service ${HTTPD} restart &> /dev/null
	echo $(date) "Httpd Restarted" >> ${LOG}
	sleep ${INTERVAL}
done

保存到 auto-start-httpd.sh
我们想要开机启动
cat >> rc.local

Post a Comment

Your email is never shared. Required fields are marked *

*
*