Linux 如何检查 JBoss 是否在 Unix 服务器上运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8761374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:57:08  来源:igfitidea点击:

How to check if JBoss is running on Unix server?

linuxunixserviceprocessjboss

提问by user1060096

I have a script below that I'd like to echo out "jboss not running" or "jboss is running" depending on whether it can find the jboss process in the process list. However, when I shut down Jboss it still executes the Else condition and says "jboss is running". If I manually do "pgrep -f jboss" it doesn't return anything, so why is it still going into the Else condition? puzzled

我在下面有一个脚本,我想根据它是否可以在进程列表中找到 jboss 进程来回显“jboss 未运行”或“jboss 正在运行”。但是,当我关闭 Jboss 时,它仍会执行 Else 条件并显示“jboss 正在运行”。如果我手动执行“pgrep -f jboss”,它不会返回任何内容,那么为什么它仍会进入 Else 状态?困惑

#!/bin/bash
if [ -z "$(pgrep -f jboss)" ]
  then
  echo "jboss is not running"
else
  echo "jboss is running"
fi 

Thanks for your help!

谢谢你的帮助!

回答by Rostislav Matl

Run jps, grepit for the line with you jboss, cutthe PIDfrom the line and check the PID.

运行JPS用grep它为符合你jboss的,PID从线并检查PID。

回答by jaypal singh

Try using the exit statusof the command -

尝试使用exit status命令的 -

#!/bin/bash

pgrep -f jboss &> /dev/null
if [ $? -eq 0 ]
  then
  echo "jboss is running"
else
  echo "jboss is not running"
fi

回答by Kevin

Instead of checking the output, just use the command:

无需检查输出,只需使用以下命令:

if pgrep -f jboss >/dev/null
  then
  echo "jboss is running"
else
  echo "jboss is not running"
fi 

回答by izzol

You cant check the jboss in that way. Because Jboss started sometimes log time like 1-2 minutes. So your script shows only process Jboss, not the "Jboss is really UP" (I mean application on JBoss).

你不能用那种方式检查 jboss。因为 Jboss 启动时有时会记录 1-2 分钟之类的时间。所以你的脚本只显示进程 Jboss,而不是“Jboss 真的 UP”(我的意思是 JBoss 上的应用程序)。

回答by RAJKUMAR.B

#! /bin/bash
 if [ -z "$(ps -ef | grep org.jboss.Main)" ]
then 
 echo "jboss is not running"
else
  echo "jboss is running"
fi

回答by John Alexander Betts

This is the best way:

这是最好的方法:

if [ -z "$(ps -ef | grep java | grep jboss)" ]
then 
 echo "JBoss is NOT running"
else
  echo "JBoss is running"
fi

回答by bushiebear

Get the process ID JBoss 7 / EAP 6:

获取进程 ID JBoss 7 / EAP 6:

pgrep -f org.jboss.as

So if you want to enhance the former example script with this:

因此,如果您想使用以下内容增强前一个示例脚本:

if [ -z "$(pgrep -f org.jboss.as)" ]
then 
 echo "JBoss is NOT running"
else
  echo "JBoss is running"
fi 

回答by Raghu K Nair

The best way is use this

最好的方法是使用这个

result=`$jboss/bin/jboss-cli.sh --connect controller=localhost:$controller_port --commands=\"read-attribute server-state\" > out 2&1`
echo "$result" | grep -q "running"
if [ $? -eq 0 ];then 
   echo "Jboss running"
fi

If you want to check if ear or war file is deployed then you could use the following command

如果要检查是否部署了 ear 或 war 文件,则可以使用以下命令

$JBOSS_HOME/bin/jboss\-cli.sh --connect controller=localhost:$PORT  --command="deployment-info --name=$YOUR_WAR_OR_EAR_FILE" 

回答by Mohammad Sadegh Rafiei

you can use below function in bash script; below function return PID of Jboss if Jboss is started yet. notice that $1 is base directory of your jboss. for example: $1=/root/app/wildfly-X.Final

您可以在 bash 脚本中使用以下函数;如果 Jboss 还没有启动,则下面的函数返回 Jboss 的 PID。注意 $1 是你的 jboss 的基本目录。例如:$1=/root/app/wildfly-X.Final

function getJbossPID() {
local pid;
local DIR=;
if [[ ! -z $DIR ]]; then
    local result=();
    jps=`jps -m`
    if [[ ! -z $jps ]]; then
        IFS='
        '
        jps_result=`jps -m`
        for jps_line_result in $jps_result; do
            result_jps=$jps_line_result;
            if [[ $result_jps == *$DIR* ]]; then
                unset IFS
                array=( $result_jps );
                pid=${array[0]};
            fi
        IFS='
        '
        done;
        unset IFS;
    fi
fi
echo $pid;
}

you can use above function same as below:

您可以使用上述功能,如下所示:

PID=$(getJbossPID $YOUR_JBOSS)

PID=$(getJbossPID $YOUR_JBOSS)