14

Linux command line:

When i execute the following command ps -ef |grep tomcat it shows me the following process

abcapp   28119     1  0 12:53 ?        00:00:19 /usr/java/jdk1.6.0_10//bin/java -Xmx256m -Dabc.log.file=/home/app/apps/rum/logs/dev.log -Dabc.config=dev -Dlog4j.configuration=file:///home/abcapp/env/abc_env/abc_env-1.2/config/log4j-webapp.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/home/abcapp/env/tomcat/tomcat-5.5-26-rum/conf/logging.properties -Djava.endorsed.dirs=/home/abcapp/env/tomcat/tomcat-5.5-26-rum/common/endorsed -classpath :/home/abcapp/env/tomcat/tomcat-5.5-26-rum/bin/bootstrap.jar:/home/abcapp/env/tomcat/tomcat-5.5-26-rum/bin/commons-logging-api.jar -Dcatalina.base=/home/abcapp/env/tomcat/tomcat-5.5-26-rum -Dcatalina.home=/home/abcapp/env/tomcat/tomcat-5.5-26-rum -Djava.io.tmpdir=/home/abcapp/env/tomcat/tomcat-5.5-26-rum/temp org.apache.catalina.startup.Bootstrap start

but when i issue following command it shows nothing

pgrep tomcat-5.5-26-rum OR pgrep "*-rum"

can some body help me how can i get tomcat process id by its name regex for "*-rum"

Thanks in advance.

d-man
  • 57,473
  • 85
  • 212
  • 296
  • People seem to be stingy when it comes to upvoting questions :) Thanks for asking this, it made it easy for me to find the answer. – Viet Norm Feb 16 '15 at 15:28

3 Answers3

24

pgrep only search for the process name without the full path (in your case only java) and without arguments.

Since tomcat-5.5-26-rum is part of the latter, i'd search the pid with

ps -ef | grep tomcat-5.5-26-rum | grep java | awk ' { print $2 } '

The double grep is useful to discard the grep pids itself

Davide Berra
  • 6,387
  • 2
  • 29
  • 50
  • 2
    If someone, like me, is looking for one line code to kill tomcat. (Assuming you have only one tomcat installed & running). It works on my Centos 7. `sudo /bin/kill -15 $(ps -ef | grep tomcat | grep java | awk ' { print $2 } ')` – Yang Apr 04 '19 at 06:41
7

Just add following line at the start of catalina.sh file

CATALINA_PID="$CATALINA_BASE"/logs/tomcat.pid

OR

CATALINA_PID=/tmp/tomcat.pid

And bounce tomcat. This will create a tomcat.pid file in the given path and put the Tomcat process pid in it.

jstricker
  • 2,132
  • 1
  • 30
  • 44
7

This worked for me:

This will give the process id of current running tomcat

echo ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'

jaydip jadhav
  • 477
  • 4
  • 5