进程 grep 给出不明确的输出
Process grep gives ambiguous output
我有这个 init.d 脚本,我在其中检查应用程序的状态,预期的输出应该是
"Application is running with PID: XXXX"
无论如何,我会告诉你我用过的代码:
ps -ef|grep -v grep |grep Jazz |grep -v "Jazz status"|awk '{ if ( != "") print "Jazz Running with PID:","; 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 ( != "") print }'
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 ( != "") print "Jazz is running with PID:",; else print "Jazz is not running";}'
#pid=$(pgrep -f Jazz) && echo "Jazz Running with PID: $pid" || echo "Jazz is not running"
}
case in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart"|"reload")
stop
start
;;
*)
echo "usage: [=15=] {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
现在我正在 运行ning 这个命令,它似乎在进程 运行ning 时给了我所需的输出,但是在 "else" 时没有给我输出进程不是运行ning:
ps -ef|grep -v grep |grep Jazz |grep -v awk |grep -v "Jazz status" |awk '{ if ( != "") print "Jazz is running with PID:",; else print "Jazz is not running";}'
您很可能正在寻找 awk。您排除了 grep,但没有排除 awk。
更好的方法是使用 pgrep 命令,例如:
echo "java is running with pids ", `pgrep java`
将列出所有 pid Java 进程 运行。它只查看进程映像名称,不查看命令行。我不确定 pgrep 的便携性如何,但它在 Linux、FreeBSD 和 OS X 上,对我来说已经足够好了。
但是查看您更新后的答案,我发现您正在寻找命令行关键字,所以就像@jlliagre 建议的那样,您应该使用 -f 开关。 pgrep 的优点是它足够聪明,可以排除自己,不像 awks 和 greps 的链。
该命令应该有效(假设 pgrep
可用):
pid=$(pgrep -f JazzSMNode01Cell) && echo "Jazz Running with PID: $pid" || echo "Jazz is not running"
我有这个 init.d 脚本,我在其中检查应用程序的状态,预期的输出应该是 "Application is running with PID: XXXX"
无论如何,我会告诉你我用过的代码:
ps -ef|grep -v grep |grep Jazz |grep -v "Jazz status"|awk '{ if ( != "") print "Jazz Running with PID:","; 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 ( != "") print }'
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 ( != "") print "Jazz is running with PID:",; else print "Jazz is not running";}'
#pid=$(pgrep -f Jazz) && echo "Jazz Running with PID: $pid" || echo "Jazz is not running"
}
case in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart"|"reload")
stop
start
;;
*)
echo "usage: [=15=] {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
现在我正在 运行ning 这个命令,它似乎在进程 运行ning 时给了我所需的输出,但是在 "else" 时没有给我输出进程不是运行ning:
ps -ef|grep -v grep |grep Jazz |grep -v awk |grep -v "Jazz status" |awk '{ if ( != "") print "Jazz is running with PID:",; else print "Jazz is not running";}'
您很可能正在寻找 awk。您排除了 grep,但没有排除 awk。 更好的方法是使用 pgrep 命令,例如:
echo "java is running with pids ", `pgrep java`
将列出所有 pid Java 进程 运行。它只查看进程映像名称,不查看命令行。我不确定 pgrep 的便携性如何,但它在 Linux、FreeBSD 和 OS X 上,对我来说已经足够好了。
但是查看您更新后的答案,我发现您正在寻找命令行关键字,所以就像@jlliagre 建议的那样,您应该使用 -f 开关。 pgrep 的优点是它足够聪明,可以排除自己,不像 awks 和 greps 的链。
该命令应该有效(假设 pgrep
可用):
pid=$(pgrep -f JazzSMNode01Cell) && echo "Jazz Running with PID: $pid" || echo "Jazz is not running"