天天看點

雙機熱備腳本

      這個服務的腳本是在備機上跑的,主機ip是192.168.2.168,在主機上多添加一個ip:192.168.2.169

每隔5秒鐘去探測下對端機機器,ping 192.168.2.169,看是否ping的通,如果ping通了,則不管。

#!/bin/bash

masterip_add=192.168.2.169

masterip=192.168.2.168

mfile=/run/nginx.pid

start_nginx(){

if [ ! -f "$mfile" ];then

nginx -s start

fi

}

c1=$(ping $masterip_add -c 1|grep Unreachable|wc -l)

if [ $c1 -gt 0 ] ; then

c2=$(ping $masterip_add -c 10|grep Unreachable|wc -l)

if [ $c2 -gt 9 ] ; then

    c3=$(ping $masterip -c 10|grep Unreachable|wc -l)

      if [ $c3 -gt 9 ] ; then

        /sbin/ifconfig eth0 $masterip netmask 255.255.255.0

start_nginx

echo "float ip to 168"

      fi

echo "can not connect"

else

c4=$(/sbin/ifconfig|grep $masterip|wc -l)

if [ $c4 -gt 0 ] ; then

    /sbin/ifconfig eth0 $masterip netmask 255.255.255.0

    echo "reset ip"

fi 

echo "connection is ok"

繼續閱讀