Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the pid of a java process under Linux

Tags:

I have a Java program running on a Linux computer, and want to find the process ID (pid) of its process. I know the ps command can provide this information, but its output is confusing because it has so much extraneous information. How can I get just the pid?


I am using MPJ library in java program for Pagerank algorithm. I compile it by

javac -cp .:$MPJ_HOME/lib/mpj.jar MpiPageRank.java 

and run by

mpjrun.sh -np 2 MpiPageRank 

where -np is number of process

Now i have to find its pid

ps -ef|grep java 

like

mpjrun.sh -np 2 MpiPageRank & sleep 2 ps -ef | grep java 

I get

pnewaska 27866 27837 99 21:28 pts/45   00:00:09 java -cp /u/pnewaska/mpj-v0_38/lib/smpdev.jar:/u/pnewaska/mpj-v0_38/lib/xdev.jar:/u/pnewaska/mpj-v0_38/lib/mpjbuf.jar:/u/pnewaska/mpj-v0_38/lib/loader2.jar:/u/pnewaska/mpj-v0_38/lib/starter.jar:/u/pnewaska/mpj-v0_38/lib/mpiExp.jar runtime.starter.MulticoreStarter /nfs/nfs1/home/pnewaska/DistributedSystems/Project3 10 smpdev useLocalLoader EMPTY MpiPageRank -i input.500k0 -n 10 -o 

Now I want to extract MpiPageRank from only 1 linux comman to get its pid ie 27866. how do i do that ?

like image 759
Shweta B. Patil Avatar asked Apr 18 '12 01:04

Shweta B. Patil


People also ask

Where is PID of Java process in Linux?

Fig: 'ps' command displaying all Java processes running on Linux machine. The red color highlight in the above figure indicates the process IDs of all Java processes running on this EC2 instance. From here, you can get hold of your application's process ID.

How do I find the Java process ID?

You can use the jps utility that is included in the JDK to find the process id of a Java process. The output will show you the name of the executable JAR file or the name of the main class. jps tool is now included in JDK/bin directory.

How do I find the process of a PID in Linux?

In this quick article, we've explored how to get the name and the command line of a given PID in the Linux command line. The ps -p <PID> command is pretty straightforward to get the process information of a PID. Alternatively, we can also access the special /proc/PID directory to retrieve process information.

How do you find a Java process on a Linux machine?

On Linux, you can view processes with the ps command. It is the simplest way to view the running processes on your system. You can use the ps command to view running Java processes on a system also by piping output to grep .


1 Answers

using ps

ps allows the user to define his own formatting for its output with the -o switch, and -C to select entries by the given command. I would go with:

ps -C java -o pid 

from the man page:

   -C cmdlist      Select by command name                    This selects the processes whose executable name is given in cmdlist.     -o format       user-defined format.                    format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify                    individual output columns. The recognized keywords are described in the STANDARD FORMAT SPECIFIERS section below. Headers                    may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as desired. If all column headers are empty                    (ps -o pid= -o comm=) then the header line will not be output. Column width will increase as needed for wide headers; this                    may be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE-WCHAN-COLUMN -o comm). Explicit width control                    (ps opid,wchan:42,cmd) is offered too. The behavior of ps -o pid=X,comm=Y varies with personality; output may be one                    column named "X,comm=Y" or two columns named "X" and "Y". Use multiple -o options when in doubt. Use the PS_FORMAT                    environment variable to specify a default as desired; DefSysV and DefBSD are macros that may be used to choose the default                    UNIX or BSD columns. 

One can get more accurate results by specifying more restrictions (ie the user under which the process runs, etc). Look at the man page for more information and other switches.

example:

$ sleep 10 & [1] 12654 $ ps -C sleep -o pid 12654 

using the shell

I don't know why you use an .sh script to run your code and not call java directly, but if in any case you use the & (background) operator, you can grab the pid through your shell, with the $! variable.

for example:

$ sleep 5 & [1] 12395 $ echo $! 12395 

same goes for the java -jar .. & command, $! will be set to the pid of the last backgrounded job.

like image 112
c00kiemon5ter Avatar answered Nov 16 '22 04:11

c00kiemon5ter