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: 7.0.56.0
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” … :)
Looks like it is WordPress escaping some portion of the code I’m pasting. Something like this: https://gist.github.com/jadzdotcom/04e7c30802157e328a11b74038192006
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 :)
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 ""'
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.
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.
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 ? :)