bash:while-do在一段时间后停止

5ktev3wc  于 2021-06-06  发布在  Kafka
关注(0)|答案(0)|浏览(312)

这似乎是一个愚蠢的问题,但我不明白为什么我的bash脚本的行为奇怪。
我有一个简单的bash脚本,它监视服务器并检查它是否正在运行。
脚本工作正常,但大约30秒后就存在了。然后我再次启动脚本,它循环一点,然后再次退出。
服务器正在持续运行,它没有停止,所以我不明白为什么我的循环在一段时间后退出。


# !/bin/bash

export ZOOKEEPER_HOST=localhost
export ZOOKEEPER_PORT=2181
export KAFKA_HOME=/home/.../server/kafka/kafka_2.11-2.0.0

# *********************************************************

# **  STOP: Kafka                                    **

# *********************************************************

stop-kafka() {
    echo "-> Stopping Kafka server..."
    #${KAFKA_HOME}/bin/kafka-server-stop.sh &
    while echo dump | nc ${ZOOKEEPER_HOST} ${ZOOKEEPER_PORT} | grep brokers
    do
        echo "Kafka server is still running..."
        sleep 1
    done
}

stop-kafka

exit 0

这是控制台上我的脚本的第一行的输出:

xxx@hp:~/workspace/xxx/xxx$ ./test-run.sh 
-> Stopping Kafka server...
    /brokers/ids/0
Kafka server is still running...
    /brokers/ids/0
Kafka server is still running...
    /brokers/ids/0
Kafka server is still running...

还有脚本退出前的最后几行:

Kafka server is still running...
    /brokers/ids/0
Kafka server is still running...
    /brokers/ids/0
Kafka server is still running...
    /brokers/ids/0
Kafka server is still running...
    /brokers/ids/0
Kafka server is still running...
    /brokers/ids/0
Kafka server is still running...
xxx@hp:~/workspace/xxx/xxx$

-->更新<--
我按照@shelleter的建议修改了我的脚本。

stop-kafka() {
    echo "-> Stopping Kafka server..."
    #${KAFKA_HOME}/bin/kafka-server-stop.sh &
    #while echo dump | nc ${ZOOKEEPER_HOST} ${ZOOKEEPER_PORT} | grep brokers
    while true
    do
        echo "Kafka server is still running..."
        echo dump | nc ${ZOOKEEPER_HOST} ${ZOOKEEPER_PORT}
        sleep 1
    done
}

我是从这个命令开始的: ./test-run.sh > test-run.log 日志文件:

...
Kafka server is still running...
SessionTracker dump:
Session Sets (3):
0 expire at Thu Jan 01 02:22:48 CET 1970:
0 expire at Thu Jan 01 02:22:51 CET 1970:
1 expire at Thu Jan 01 02:22:54 CET 1970:
    0x10000491a850000
ephemeral nodes dump:
Sessions with Ephemerals (1):
0x10000491a850000:
    /controller
    /brokers/ids/0
Kafka server is still running...  <-- missing content after this line
Kafka server is still running...
SessionTracker dump:
Session Sets (3):
0 expire at Thu Jan 01 02:22:48 CET 1970:
0 expire at Thu Jan 01 02:22:51 CET 1970:
1 expire at Thu Jan 01 02:22:54 CET 1970:
    0x10000491a850000
ephemeral nodes dump:
Sessions with Ephemerals (1):
0x10000491a850000:
    /controller
    /brokers/ids/0
...

在一些循环之后,您可以看到“payload”内容丢失了。这是一个窃听器吗 nc ?
你能给我建议另一个间接层次吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题