Tomcat: Getting the process ID

If you want to get the process id (PID) of your tomcat you can do something like this:

ps h -C java -o "%p:%a" | grep catalina | cut -d: -f1

If you want to kill this tomcat, because it didn’t shut down properly you can use the following:

kill -9 `ps h -C java -o "%p:%a" | grep catalina | cut -d: -f1`

A better way is to set the environment variable CATALINA_PID. If this variable is set the PID is written in the specified file (here catalina.pid) on tomcat startup.

export CATALINA_PID=/opt/tomcat/catalina.pid
/opt/tomcat/bin/startup.sh

Now killing the tomcat is also easier: You can can stop tomcat with -force, which will kill the tomcat process after shutdown (-force only works if CATALINA_PID is set).

export CATALINA_PID=/opt/tomcat/catalina.pid
/opt/tomcat/bin/shutdown.sh -force

(If you set CATALINA_PID in your .bashrc you don’t have to set it before startup/shutdown)

Advertisements
Posted in Tomcat. Tags: . 1 Comment »

One Response to “Tomcat: Getting the process ID”

  1. Steve Dustin Says:

    Thank you Martin! This saved me from having to write my own script for searching for the Catalina PID. Catalina writes PIDs to a file via CATALINA_PID, but I’ve redirected its output to a custom log splitter. Unfortunately, catalina was writing the PID of that log splitter instead of the tomcat instance, and writing the wrong PID to the PID file in /tmp/. This should help fix that, thank you!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: