Tuesday, May 21, 2013

Prefer jps Over ps When Looking for Java PIDs

There are several tools that come with the Oracle's and the Open JDK versions that are not widely used. These tools add a great deal of power to the JDK as a platform. Unfortunately, many of them go unused and developers and sysadmins alike spend a lot of time solving some operational problems on their own.

One common problem on most Linux/Unix based systems is seeing what Java processes are running. Most of the time, you'll run ps to see what's running:

$ ps u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
andy      6539  0.0  0.1  24772  5680 pts/0    Ss+  May17   0:01 /bin/bash
andy      7359  0.0  0.1  24752  5688 pts/2    Ss   May17   0:00 /bin/bash
andy      7448  0.0  0.1  24748  5272 pts/3    Ss+  May17   0:00 /bin/bash
andy     14490  1.0  6.3 2554896 248276 pts/2  Sl   10:13   0:24 /usr/lib/jvm/sun-java-6
andy     15063  0.0  0.0  18172  1256 pts/2    R+   10:50   0:00 ps u



This shows not only your JVM process but everything else you're running. That's fine when you're not running a lot of processes at once, but if you are, then you're likely using grep to trim the list down.

There's a tool with the JDK called jps that works like ps except that it will show just the JVM processes that are running. When jps is run with no options, it will show you the process ID and the class name or Jar file that was invoked.

$ jps
14490 Bootstrap
10051 org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
15114 Jps


There are a few options that you can review on your own, but the power of this tool is great not only for development (what PID is my Tomcat process running on?) but it can also be a useful remote application tool.