Home > bash, coding, cygwin, debian, java, jvm, linux, mac os, os > Determine which Tomcat version is running

Determine which Tomcat version is running

Determine process id

First we determine the process id(s) of the running Tomcat instance(s).

We can grep the running process list for ‘catalina.home’:

pgrep -f 'catalina.home'

This might yield more than one pid.

Or we can search by port (8080 is the default, adjust if necessary). The following commands will likely require root privileges:

lsof -t -i :8080

Alternatively, for example if lsof is not installed:

fuser 8080/tcp

Or yet another way, using netstat (or its “ss” replacement):

netstat -nlp | grep 8080
ss -nlp | grep 8080

Determine catalina.home

For the process id(s) determined above, we look at process details:

ps -o pid,uid,cmd -p [pidlist] | cat

For each specified pid, this shows the uid (system user) and the full command line of the process.

Typically the command line will contain something like “-Dcatalina.home=[path]” and that path is the catalina.home system property of the Java process.

Alternatively – with Java 7 and later – we can use the JDK command “jcmd” to query the JVM process for its system properties:

sudo -u [uid] jcmd [pid] VM.system_properties \
   | grep '^catalina.home' \
   | cut -f2 -d'='

Determine version

Now we can finally determine which Tomcat version is installed under the catalina.home path:

[catalina.home]/bin/catalina.sh version \
   | grep '^Server number:'

Note: Please replace [catalina.home] with the path you determined above.

The final output should be something like this:

Server number:

  1. August 9, 2016 at 11:43

    I tried the corrected version one-liner – from your gist – but getting an error. I guess we could say that “details are left as an exercise to the reader” … :)

  2. August 8, 2016 at 22:56

    Looks like it is WordPress escaping some portion of the code I’m pasting. Something like this: https://gist.github.com/jadzdotcom/04e7c30802157e328a11b74038192006

  3. August 8, 2016 at 22:53

    It doesn’t help that there is a typo in that example one liner.

    ps aux |grep -v grep | grep -oP ‘(?<=catalina.home=)\S*' | xargs -I {} sh -c 'echo "{}" &/bin/catalina.sh version |grep "Server number" && echo ""'

    I have no doubt you could continue to patch together some more regex and xarg arguments, but its already a bit ugly :)

  4. August 8, 2016 at 11:07

    Oliver – good job explaining each step. For the lazy, it might be useful to include a one line that puts each of those steps together?

    Something like:

    ps aux |grep -v grep | grep -oP ‘(?<=catalina.home=)\S*' | xargs -I {} sh -c 'echo "{}" &/bin/catalina.sh version |grep "Server number" && echo ""'

    • August 8, 2016 at 20:35

      Thanks for the feedback, Joe. I wanted to keep the steps separate because of the potential usefulness of the lsof and jcmd steps. But if there is only one Tomcat running then your one-liner seems like a handy thing to have, maybe even as a shell alias or function.

      • August 8, 2016 at 21:11

        I do agree on the useful of the breakdown and the knowledge of each step. The example I posted should echo the catalina.home directory and then the Tomcat version – so it will still work with multiple tomcats running.

      • August 8, 2016 at 22:34

        Ok, I see. I guess I need to actually try out your command line to see it in action. What about including the port listened to? Can you somehow tweak that into the one-liner ? :)

  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: