gpt4 book ai didi

linux - 进程 grep 给出不明确的输出

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:05 28 4
gpt4 key购买 nike

我有这个 init.d 脚本,我正在检查应用程序的状态,预期的输出应该是“应用程序正在使用 PID 运行:XXXX”

无论如何,我会向您展示我使用过的代码:

ps -ef|grep -v grep |grep Jazz |grep -v "Jazz status"|awk '{ if ( $2 != "") print "Jazz Running with PID:",$2"; else print "Jazz not running";}'

当我在终端上运行它时,我得到以下输出:

    Jazz Running with PID: 6261
Jazz Running with PID: 30077

当我删除打印语句并输出 $2 时,我得到以下输出:

#ps -ef|grep -v grep |grep Jazz |grep -v "Jazz status"|awk '{ if ( $2 != "") print $2}'   
300077

我希望输出是

Jazz Running with PID: 300773

我很困惑为什么我在第一种情况下会得到两个 PID?P.S 第一个 PID 不断变化,类似于向我显示命令本身的 PID。


编辑:输出:ps -ef|grep Jazz

netcool  12744     1  5 13:03 ?        00:06:06 /opt/IBM/tivoli/webSphere/AppServer/java/bin/java -Declipse.security -Dwas.status.socket=38692 -Dosgi.install.area=/opt/IBM/tivoli/webSphere/AppServer -Dosgi.configuration.area=/opt/IBM/tivoli/jazzSM/profile/servers/server1/configuration -Djava.awt.headless=true -Dosgi.framework.extensions=com.ibm.cds,com.ibm.ws.eclipse.adaptors -Xshareclasses:name=webspherev85_1.6_64_%g,nonFatal -Xbootclasspath/p:/opt/IBM/tivoli/webSphere/AppServer/java/jre/lib/ibmorb.jar -classpath /opt/IBM/tivoli/jazzSM/profile/properties:/opt/IBM/tivoli/webSphere/AppServer/properties:/opt/IBM/tivoli/webSphere/AppServer/lib/startup.jar:/opt/IBM/tivoli/webSphere/AppServer/lib/bootstrap.jar:/opt/IBM/tivoli/webSphere/AppServer/lib/jsf-nls.jar:/opt/IBM/tivoli/webSphere/AppServer/lib/lmproxy.jar:/opt/IBM/tivoli/webSphere/AppServer/lib/urlprotocols.jar:/opt/IBM/tivoli/webSphere/AppServer/deploytool/itp/batchboot.jar:/opt/IBM/tivoli/webSphere/AppServer/deploytool/itp/batch2.jar:/opt/IBM/tivoli/webSphere/AppServer/java/lib/tools.jar -Dibm.websphere.internalClassAccessMode=allow -Xms512m -Xmx1024m -Xcompressedrefs -Xscmaxaot4M -Xscmx60M -Dws.ext.dirs=/opt/IBM/tivoli/webSphere/AppServer/java/lib:/opt/IBM/tivoli/jazzSM/profile/classes:/opt/IBM/tivoli/webSphere/AppServer/classes:/opt/IBM/tivoli/webSphere/AppServer/lib:/opt/IBM/tivoli/webSphere/AppServer/installedChannels:/opt/IBM/tivoli/webSphere/AppServer/lib/ext:/opt/IBM/tivoli/webSphere/AppServer/web/help:/opt/IBM/tivoli/webSphere/AppServer/deploytool/itp/plugins/com.ibm.etools.ejbdeploy/runtime -Dderby.system.home=/opt/IBM/tivoli/webSphere/AppServer/derby -Dcom.ibm.itp.location=/opt/IBM/tivoli/webSphere/AppServer/bin -Djava.util.logging.configureByServer=true -Duser.install.root=/opt/IBM/tivoli/jazzSM/profile -Djava.ext.dirs=/opt/IBM/tivoli/webSphere/AppServer/tivoli/tam:/opt/IBM/tivoli/webSphere/AppServer/java/jre/lib/ext -Djavax.management.builder.initial=com.ibm.ws.management.PlatformMBeanServerBuilder -Dpython.cachedir=/opt/IBM/tivoli/jazzSM/profile/temp/cachedir -Dwas.install.root=/opt/IBM/tivoli/webSphere/AppServer -Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager -Dserver.root=/opt/IBM/tivoli/jazzSM/profile -Dcom.ibm.security.jgss.debug=off -Dcom.ibm.security.krb5.Krb5Debug=off -Dfas.install.location=/opt/IBM/tivoli/jazzSM/admin -Dfas.ui.install.location=/opt/IBM/tivoli/jazzSM/admin_ui -Djava.library.path=/opt/IBM/tivoli/webSphere/AppServer/lib/native/linux/x86_64/:/opt/IBM/tivoli/webSphere/AppServer/java/jre/lib/amd64/default:/opt/IBM/tivoli/webSphere/AppServer/java/jre/lib/amd64:/opt/IBM/tivoli/webSphere/AppServer/bin:/usr/lib:/opt/IBM/tivoli/jazzSM/reporting/cognos/bin64: -Djava.endorsed.dirs=/opt/IBM/tivoli/webSphere/AppServer/endorsed_apis:/opt/IBM/tivoli/webSphere/AppServer/java/jre/lib/endorsed:/opt/IBM/tivoli/webSphere/AppServer/endorsed_apis:/opt/IBM/tivoli/webSphere/AppServer/java/jre/lib/endorsed -Djava.security.auth.login.config=/opt/IBM/tivoli/jazzSM/profile/properties/wsjaas.conf -Djava.security.policy=/opt/IBM/tivoli/jazzSM/profile/properties/server.policy com.ibm.wsspi.bootstrap.WSPreLauncher -nosplash -application com.ibm.ws.bootstrap.WSLauncher com.ibm.ws.runtime.WsServer /opt/IBM/tivoli/jazzSM/profile/config JazzSMNode01Cell JazzSMNode01 server1
netcool 19735 19678 0 14:50 pts/0 00:00:00 grep Jazz

编辑我使用的完整 init.d 脚本:

#!/bin/sh

USER="xxxxxxx"
PASSWORD="xxxxxx"
start() {
su - netcool -c "/opt/IBM/tivoli/jazzSM/profile/bin/startServer.sh server1"

RETVAL=$?
return $RETVAL
}
stop() {
su - netcool -c "/opt/IBM/tivoli/jazzSM/profile/bin/stopServer.sh server1 -username ${USER} -password ${PASSWORD}"

RETVAL=$?
return $RETVAL
}
status() {
ps -ef|grep -v grep |grep Jazz |grep -v awk |grep -v "Jazz status" |awk '{ if ( $2!= "") print "Jazz is running with PID:",$2; else print "Jazz is not running";}'
#pid=$(pgrep -f Jazz) && echo "Jazz Running with PID: $pid" || echo "Jazz is not running"
}
case $1 in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart"|"reload")
stop
start
;;

*)
echo "usage: $0 {start|stop|status|restart|reload}"
RETVAL=1
esac
exit 0

希望对你有帮助


更新根据@jlliagre 的建议,运行以下命令仍然给我一个以上的 PID 作为输出:

[root@hjkpnlinst04 ~]# pid=$(pgrep -f jazzSM) && echo "Jazz Running with PID: $pid" || echo "Jazz is not running"
Jazz Running with PID: 12744
13176
13299

现在我正在运行这个命令,它似乎在进程运行时给了我所需的输出,但是当进程没有运行时却没有给我“else”输出:

ps -ef|grep -v grep |grep Jazz |grep -v awk |grep -v "Jazz status" |awk '{ if ( $2!= "") print "Jazz is running with PID:",$2; else print "Jazz is not running";}'

最佳答案

该命令应该有效(假设 pgrep 可用):

pid=$(pgrep -f JazzSMNode01Cell) && echo "Jazz Running with PID: $pid" || echo "Jazz is not running"

关于linux - 进程 grep 给出不明确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33665324/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com