1

I have 2 tomcat's running, how do I get the pid if both have similar names?

ps -ef | grep java
root     12952     1  0 10:01 pts/0    00:00:03 /usr/lib/jvm/jre/bin/java -Djava.util.logging.config.file=/usr/local/realbid/tomcat-realbid-ws/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/realbid/tomcat-realbid-ws/endorsed -classpath /usr/local/realbid/tomcat-realbid-ws/bin/bootstrap.jar -Dcatalina.base=/usr/local/realbid/tomcat-realbid-ws -Dcatalina.home=/usr/local/realbid/tomcat-realbid-ws -Djava.io.tmpdir=/usr/local/realbid/tomcat-realbid-ws/temp org.apache.catalina.startup.Bootstrap start
root     12995     1  0 10:02 pts/0    00:00:03 /usr/lib/jvm/jre/bin/java -Djava.util.logging.config.file=/usr/local/realbid/tomcat-realbid/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/realbid/tomcat-realbid/endorsed -classpath /usr/local/realbid/tomcat-realbid/bin/bootstrap.jar -Dcatalina.base=/usr/local/realbid/tomcat-realbid -Dcatalina.home=/usr/local/realbid/tomcat-realbid -Djava.io.tmpdir=/usr/local/realbid/tomcat-realbid/temp org.apache.catalina.startup.Bootstrap start
root     13317 12252  0 10:12 pts/0    00:00:00 grep java

If I use basic grep I get both the pid(s)

echo `ps aux | grep  'tomcat-realbid' | grep -v grep | awk '{ print $2 }'`
12952 12995

Is there a way to get only the pid of tomcat-realbid?

5 Answers 5

1

You could use an inverse grep so perhaps:

ps aux | grep 'tomcat-realbid' | grep -v 'tomcat-realbid-ws'

This would basically filter for the processes with tomcat-realbid and then filter again removing any that have tomcat-realbid-ws, you are already using an inverse grep to remove the original grep. The end result may look like this:

echo ps aux | grep  'tomcat-realbid' | grep -v 'tomcat-realbid-ws' | grep -v grep | awk '{ print $2 }'
1
  • yeah , forgot about the v option , it works now , Thanks Mar 10, 2015 at 10:52
0

Try

ps auxwww | grep "/tomcat-realbid/"
0

A bit shorter:

pgrep 'tomcat-realbid[^(-ws)]*$'

Searches for tomcat-realbid without the ending -ws and prints the pid.

0

Maybe try :

pgrep tomcat-realbid
1
  • pgrep does not return any output [root@kibana bin]# echo ps aux | pgrep tomcat-realbid | grep -v grep | awk '{ print $2 }' Mar 10, 2015 at 10:40
0

Normally awk can do what grep does, so you can join all the commands in just one:

ps -ef | awk '/java/ && /tomcat-realbid / {print $2}'

This gets all those lines containing both java and tomcat-realbid (note the space at the end, just to get the one you wanted). For those matchines lines, it prints its 2nd field.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .