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?

chaos
  • 1,445
  • 1
  • 14
  • 21
user3789893
  • 11
  • 1
  • 4

5 Answers5

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 }'
Ned
  • 11
  • 2
0

Maybe try :

pgrep tomcat-realbid
HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Kharec
  • 9
  • 2
  • pgrep does not return any output [root@kibana bin]# echo `ps aux | pgrep tomcat-realbid | grep -v grep | awk '{ print $2 }'` – user3789893 Mar 10 '15 at 10:40
0

Try

ps auxwww | grep "/tomcat-realbid/"
Janne Pikkarainen
  • 31,454
  • 4
  • 56
  • 78
0

A bit shorter:

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

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

chaos
  • 1,445
  • 1
  • 14
  • 21
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.

fedorqui
  • 248
  • 4
  • 17