zoukankan      html  css  js  c++  java
  • linux shell利用nc命令检测机器端口 + supervisorctl检测进程状态

    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=$?
  • 相关阅读:
    ABAP开发者上云的时候到了
    1074. 宇宙无敌加法器(20)
    1073. 多选题常见计分法(20)
    1072. 开学寄语(20)
    1071. 小赌怡情(15)
    1049. Counting Ones (30)
    1047. Student List for Course (25)
    1044. Shopping in Mars (25)
    1043. Is It a Binary Search Tree (25)
    1040. Longest Symmetric String (25)
  • 原文地址:https://www.cnblogs.com/subendong/p/15574737.html
Copyright © 2011-2022 走看看