如何让我的shell脚本检测多个调用的状态更改?

时间:2017-06-07 16:27:10

标签: linux bash shell

我正在使用以下"脚本"监视服务器上的端口:

l_TELNET=`echo "quit" | telnet server.domain.tld 12345 | grep "Escape character is"`
if [ "$?" -ne 0 ]; then
  echo "Connection to $SERVER on port $PORT failed"
    #Something happens here if Service (port) down
  exit 1
else
    #Something happens here if Service (port) up
  exit 0
fi

如何将其修改为仅执行" Service down"部分第一次时间检测到服务已关闭(直到它下一次备份),而不是服务继续停止时每五分钟一次?

1 个答案:

答案 0 :(得分:1)

使用临时文件怎么样?

l_TELNET=`echo "quit" | telnet server.domain.tld 12345 | grep "Escape character is"`
if [ "$?" -ne 0 ]; then
    if  [ ! -f /tmp/12345down]; then
        echo "Connection to $SERVER on port $PORT failed"
        #Something happens here if Service (port) down
        touch /tmp/12345down
        exit 1
    fi
    exit 0
else 
    #Something happens here if Service (port) up
    if  [ -f /tmp/12345down]; then
        rm -f /tmp/12345down
    fi
    exit 0
fi