Anonymous Anonymous - 3 years ago 255
Bash Question

Process grep gives ambiguous output

I have this init.d script where i am checking the status of an application, The expected output should be
"Application is running with PID: XXXX"

Anyways, i'll show you the code that i have used:

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";}'

When i run this on the terminal i get the following output:

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

When i remove the print statement and output $2 i get the following output:

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

I expect the output to be

Jazz Running with PID: 300773

I am stumped as to why i am getting two PID's in the first case?
P.S the first PID keeps changing, similar to showing me the PID of the command itself.

Output of:
ps -ef|grep Jazz

netcool 12744 1 5 13:03 ? 00:06:06 /opt/IBM/tivoli/webSphere/AppServer/java/bin/java -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, -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/ -Dderby.system.home=/opt/IBM/tivoli/webSphere/AppServer/derby -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 -Dpython.cachedir=/opt/IBM/tivoli/jazzSM/profile/temp/cachedir -Dwas.install.root=/opt/IBM/tivoli/webSphere/AppServer -Dserver.root=/opt/IBM/tivoli/jazzSM/profile -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 -nosplash -application /opt/IBM/tivoli/jazzSM/profile/config JazzSMNode01Cell JazzSMNode01 server1
netcool 19735 19678 0 14:50 pts/0 00:00:00 grep Jazz

Full init.d script that i have used:


start() {
su - netcool -c "/opt/IBM/tivoli/jazzSM/profile/bin/ server1"

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

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

echo "usage: $0 {start|stop|status|restart|reload}"
exit 0

Hope this helps

Answer Source

Most likely you are finding awk. You are excluding grep, but not awk. Better way to do this is using the pgrep command, e.g.:

echo "java is running with pids ", `pgrep java`

will list all pids Java processes are running with. It looks at the process image name only, not on the command line. I am not sure how portable pgrep is but it is there on Linux, FreeBSD and OS X, so good enough for me.

But looking at your updated answer I see that you are looking for the command line keyword, so like @jlliagre suggests, you should use -f switch. The advantage of pgrep is that it is smart enough to exclude itself, unlike the chain of awks and greps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download