bash:一会儿-片刻后停止

时间:2018-10-30 11:22:32

标签: bash loops while-loop apache-kafka apache-zookeeper

看来这是一个愚蠢的问题,但我看不出为什么我的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$ 

->更新<-

我按照@shellter的建议修改了脚本。

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
...

经过一些循环后,您可以看到缺少“有效载荷”内容。 这是nc中的错误吗?

您能建议我再间接化吗?

0 个答案:

没有答案