• 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=$?
  • 相关阅读:
    AOP面向方面编程
    Struts2基于注解的Action配置
    地图api汇总
    Visual C++ 嵌入汇编代码
    C# ASP.net中用到的JWT身份验证
    Asp.Net Forms 身份验证
    .Net 面试常见问题
    Web Api 自动生成帮助文档
    .Net常见的一些区别
    创建Silverlight 5浏览器内受信应用
  • 原文地址:https://www.cnblogs.com/subendong/p/15574737.html
Copyright © 2020-2023  润新知