因squid对多核服务器支持不够理想,所以在每台freebsd服务器上(4核)开启了3个squid
平稳的运行了一段时间,有一天客服反映网页无法打开,然后就对每台squid服务器进行检查,发现有台服务器其中的一个squid进程死掉了,重新启动该squid,一切正常,为了及时发现squid进程故障并重启squid,特写了以下脚本
定时器,每过10分钟检查一次,无限循环
vi /root/restart_squid.sh
#!/bin/sh
while true
do
Squid1_IP=`awk '/ifconfig_lo0_alias0/{print $2}' /etc/rc.conf`
Squid1Num=`netstat -an | grep -c $Squid1_IP`
if [ $Squid1Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid1 start
/usr/local/etc/rc.d/squid1 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_1 started" >> /var/restart_squid.log
fi
Squid2_IP=`awk '/ifconfig_lo0_alias1/{print $2}' /etc/rc.conf`
Squid2Num=`netstat -an | grep -c $Squid2_IP`
if [ $Squid2Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid2 start
/usr/local/etc/rc.d/squid2 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_2 started" >> /var/restart_squid.log
fi
Squid3_IP=`awk '/ifconfig_lo0_alias2/{print $2}' /etc/rc.conf`
Squid3Num=`netstat -an | grep -c $Squid3_IP`
if [ $Squid3Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid3 start
/usr/local/etc/rc.d/squid3 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_3 started" >> /var/restart_squid.log
fi
sleep 10m
done
while true
do
Squid1_IP=`awk '/ifconfig_lo0_alias0/{print $2}' /etc/rc.conf`
Squid1Num=`netstat -an | grep -c $Squid1_IP`
if [ $Squid1Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid1 start
/usr/local/etc/rc.d/squid1 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_1 started" >> /var/restart_squid.log
fi
Squid2_IP=`awk '/ifconfig_lo0_alias1/{print $2}' /etc/rc.conf`
Squid2Num=`netstat -an | grep -c $Squid2_IP`
if [ $Squid2Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid2 start
/usr/local/etc/rc.d/squid2 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_2 started" >> /var/restart_squid.log
fi
Squid3_IP=`awk '/ifconfig_lo0_alias2/{print $2}' /etc/rc.conf`
Squid3Num=`netstat -an | grep -c $Squid3_IP`
if [ $Squid3Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid3 start
/usr/local/etc/rc.d/squid3 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_3 started" >> /var/restart_squid.log
fi
sleep 10m
done
chmod +x /root/restart_squid.sh
/root/restart_squid.sh
也可以用计划任务来实现
vi /root/restart_squid.sh
#!/bin/sh
Squid1_IP=`awk '/ifconfig_lo0_alias0/{print $2}' /etc/rc.conf`
Squid1Num=`netstat -an | grep -c $Squid1_IP`
if [ $Squid1Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid1 start
/usr/local/etc/rc.d/squid1 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_1 started" >> /var/restart_squid.log
fi
Squid2_IP=`awk '/ifconfig_lo0_alias1/{print $2}' /etc/rc.conf`
Squid2Num=`netstat -an | grep -c $Squid2_IP`
if [ $Squid2Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid2 start
/usr/local/etc/rc.d/squid2 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_2 started" >> /var/restart_squid.log
fi
Squid3_IP=`awk '/ifconfig_lo0_alias2/{print $2}' /etc/rc.conf`
Squid3Num=`netstat -an | grep -c $Squid3_IP`
if [ $Squid3Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid3 start
/usr/local/etc/rc.d/squid3 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_3 started" >> /var/restart_squid.log
fi
Squid1Num=`netstat -an | grep -c $Squid1_IP`
if [ $Squid1Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid1 start
/usr/local/etc/rc.d/squid1 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_1 started" >> /var/restart_squid.log
fi
Squid2_IP=`awk '/ifconfig_lo0_alias1/{print $2}' /etc/rc.conf`
Squid2Num=`netstat -an | grep -c $Squid2_IP`
if [ $Squid2Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid2 start
/usr/local/etc/rc.d/squid2 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_2 started" >> /var/restart_squid.log
fi
Squid3_IP=`awk '/ifconfig_lo0_alias2/{print $2}' /etc/rc.conf`
Squid3Num=`netstat -an | grep -c $Squid3_IP`
if [ $Squid3Num -lt 10 ] ; then
/usr/local/etc/rc.d/squid3 start
/usr/local/etc/rc.d/squid3 restart
adddate=`date +%Y-%m-%d`" "`date +%H:%M:%S`
echo "$adddate squid_3 started" >> /var/restart_squid.log
fi
chmod +x /root/restart_squid.sh
crontab -e
*/10 * * * * root /root/restart_squid.sh
本文转自king_819 51CTO博客,原文链接:http://blog.51cto.com/kerry/291714,如需转载请自行联系原作者