192.168.1.101
#!/bin/bash function checkStatus() {
# 检测rfidConnecter进程的状态,返回的是一串字符串 str1=$(supervisorctl status rfidConnecter)
# 判断进程是否存活的一个字符串 str2="RUNNING"
# 判断检测结果中是否包含RUNNING字符串,如果包含返回非空字符串 r=$(echo $str1 | grep "${str2}") if [[ "$r" != "" ]];then return 0 else return 1 fi } step=2 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do sleep $step nc -v -w 5 -z 192.168.1.103 5000 &> /dev/null age=$? checkStatus status=$? if (( $age == 0 )); then if (( $status == 0 )); then echo "0_stop_关闭本地5000" > /z.txt supervisorctl stop rfidConnecter else echo "0_stop_无需关闭5000" > /z.txt fi else if (( $status == 1 )); then echo "1_start_开启本地5000" > /z.txt supervisorctl start rfidConnecter else echo "1_start_无需开启5000" > /z.txt fi fi done exit 0
192.168.1.103
#!/bin/bash function checkStatus() { str1=$(supervisorctl status rfidConnecter) str2="RUNNING" r=$(echo $str1 | grep "${str2}") if [[ "$r" != "" ]];then return 0 else return 1 fi } step=4 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do sleep $step nc -v -w 5 -z 192.168.1.101 5000 &> /dev/null age=$? checkStatus status=$? if (( $age == 0 )); then if (( $status == 0 )); then echo "0_stop_关闭本地5000" > /z.txt supervisorctl stop rfidConnecter else echo "0_stop_无需关闭5000" > /z.txt fi else if (( $status == 1 )); then echo "1_start_开启本地5000" > /z.txt supervisorctl start rfidConnecter else echo "1_start_无需开启5000" > /z.txt fi fi done exit 0
说明:“$?”表示获取上一条命令执行的结果,如下:
# nc判断主机端口是否开放 nc -v -w 5 -z 192.168.1.101 5000 &> /dev/null # 接收nc判断结果,0.开放;1.关闭 age=$? # 调用自定义方法,检查本地进程是否OK checkStatus # 接收检查结果,0.OK;1.NG status=$?