通过/etc/hosts实现域名自动切换

门店通过域名访问系统,即服务器,也可以支持在门店没有网络或者服务器挂掉的时候能够针对堂食和自取的订单下单,门店的收银电脑安装了系统的离线服务,需要能够自行切换。

收银电脑配置,需要将收银电脑的局域网IP固定设置为192.168.5.200

#!/bin/bash

if [ ! -f /home/lks/oos_restart_countdown ];then
	touch /home/lks/oos_restart_countdown
	echo "0">/home/lks/oos_restart_countdown
fi


domain="sos.xxxxxxx.com"
url='http://127.0.0.1:8080/keepAlive'
result=`curl $url`
echo $result
count=`cat /etc/hosts|grep "$domain"|wc -l`
echo "the file /etc/hosts contain string \"$domain\" num: $count \n"
# 记录oos重启次数
if [ ! -f /home/lks/oos_restart_time ];then
	touch /home/lks/oos_restart_time
	echo "0">/home/lks/oos_restart_time
fi
if [ "$result"x = "alive"x ]
then
        if [ $count -eq 0 ]
        then
                echo "add string '127.0.0.1   sos.xxxxxxx.com' to /etc/hosts"
#               echo "127.0.0.1       $domian" >> /etc/hosts
                sed -i "$ a127.0.0.1       $domain" /etc/hosts
        fi
        echo "0">/home/lks/oos_restart_time
        echo "0">/home/lks/oos_restart_countdown
else
        if [ $count -gt 0 ]
        then
                echo "del '127.0.0.1   sos.xxxxxxx.com'  /etc/hosts"
                sed -i "/$domain/d" /etc/hosts
        fi
        # 服务假死,重启
        countdown=$(cat /home/lks/oos_restart_countdown)
		if [ $countdown -ne 0 ];then
			echo `expr $countdown - 1`>/home/lks/oos_restart_countdown
			return
		fi
        if [ -f /usr/lib/oos/oos.jar ];then
        	num=$(cat /home/lks/oos_restart_time)
        	if [ $num -lt 3 ];then
        		systemctl restart oos
        		echo `expr $num + 1`>/home/lks/oos_restart_time
        		# 三分钟后在重启
        		echo "3">/home/lks/oos_restart_countdown
        	fi
        fi
fi

厨房、水吧

#!/bin/sh

ip=192.168.5.200
count=`cat /etc/hosts|grep "$ip"|wc -l`   #判断是否加了192.168.5.200 sos.xxxxxxx.com
echo "the file /etc/hosts contain string \"$ip\" num: $count \n"

num=1    #定义初始值
while [ $num -le 3 ]  
do
   if ping -c 1 $ip >/dev/null
   then
      echo "$ip Ping is success"  #if 判断如果ping通 则返回success 
      if [ $count -eq 0 ]
      then
        #echo "$ip       sos.lacesar.com" >> /etc/hosts
                sed -i "$ a$ip       sos.xxxxxxx.com" /etc/hosts
      fi
      break
   else
      #$FALL[$num]=$ip                             # 反之 则失败次数+1
      echo "this is $num time failure!"
      num=`expr $num + 1`
   fi
done
if [ $num -ge 3 ]          # if判断如果失败次数等于3 则返回Ping is failure
then
   echo "$ip Ping is failure!"
   sed -i "/$ip/d" /etc/hosts
fi

 

你可能感兴趣的:(linux,ubuntu)