仅启动 jboss-cli 一次

Start jboss-cli only once

我想创建一个 bash/shell 脚本,它使用 jboss-cli 监控 Wildfly 的 JVM 内存使用情况 因此,我需要获取主机并在 for 循环中获取每个主机的 Wildfly 服务器。但是,starting/connecting jboss-cli 需要几秒钟并强调 CPU。这使得脚本非常慢。在交互式 jboss-cli 会话中发送命令非常快。

有没有办法只连接到 jboss-cli 一次并将输入命令发送到该活动会话?我需要命令的输出才能继续执行脚本。

当前脚本:

# List all hosts:
hosts="$(jboss-cli.sh -c --controller=servername:9990 --command=":read-children-names(child-type=host)" | grep "        " | awk '{print }' | sed 's/,//g' | sed 's/"//g')"
# Loop through hosts results:
for host in $hosts
{       
        #List all servers:
        servers="$(jboss-cli.sh -c --controller=servername:9990 --command="/host=$host:read-children-names(child-type=server)" | grep "        " | awk '{print }' | sed 's/,//g' | sed 's/"//g')"
        # Loop through server results:
        for server in $servers
        {
            # check if server is running:
            serverstate=$(jboss-cli.sh -c --controller=servername:9990 --command="/host=$host/server=$server:read-attribute(name=server-state)" | grep "result" | awk '{print }' | sed 's/"//g')

            if [ $serverstate = "running" ]
            then
                #Do a check etc. etc.
            fi
        }       
}

遇到了类似的问题。我的解决方案是在 ksh 中使用协程,我不知道在 bash 中是否可以像你的标志一样(但如果安装了 ksh,你仍然可以使用它)。至少它可以给你一些指点。

#!/bin/ksh

#connecting using coroutine -- Note the |& at the end
{JBOSS_HOME}/bin/jboss-cli.sh --connect --controller={Master_IP}:{Master_Port} --user={UID} --password={Password} |&
#sending request to coroutine
print -p "ls -l /host\n"
#Reading from corouting
while read -p answer; do
##Need to verify when stop reading -- after the prompt is back
    if [[ $answer != *domain*:* ]]; then
    ## If it's not an error, concatenate, space delimited, the response
        if [[ $answer == *WFLYCTL* ]]; then
            print "$(date +"%Y%m%d%H%M") - FATAL - Error detected - Exiting"
            print "$(date +"%Y%m%d%H%M") - Error detected : $answer"
            exit
        else
            Dummy+=$answer" "
        fi
    elif [[ locFlag -eq 1 ]]; then
        locFlag=0
        break
    else
        locFlag=1
    fi
done
##Response to array
SvrArray=( $Dummy )
##Do anything with the response, here just printing
for i in ${!SvrArray[@]}; do
        print "$i. ${SvrArray[$i]}"
done