在Linux下查找java进程的pid

在Linux下查找java进程的pid

本文介绍了在Linux下查找java进程的pid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在Java程序中使用MPJ库为Pagerank算法。
我将它编译为

  

使用 ps



ps 允许用户使用 -o定义自己的输出格式开关和 -C 按给定命令选择条目。我会继续:

  ps -C java -o pid 



  -C cmdlist通过命令选择名称
这选择在cmdlist中给定可执行文件名称的进程。

-o格式的用户定义格式。
格式是以空格分隔或逗号分隔列表形式的单个参数,它提供了一种指定
个别输出列的方式。公认的关键字在下面的STANDARD FORMAT SPECIFIERS部分进行了描述。可根据需要重新命名标题
(ps -o pid,ruser = RealUser -o comm = Command)。如果所有列标题都是空的
(ps -o pid = -o comm =),那么标题行将不会被输出。宽头的宽度将根据需要增加;这个
可能用于扩展WCHAN(ps -o pid,wchan = WIDE-WCHAN-COLUMN -o comm)等列。显式宽度控制
(ps opid,wchan:42,cmd)也提供。 ps -o pid = X,comm = Y的行为因人而异;输出可以是一个名为X,comm = Y的
列或名为X和Y的两列。如有疑问,请使用多个-o选项。根据需要使用PS_FORMAT
环境变量指定默认值; DefSysV和DefBSD是可用于选择缺省
UNIX或BSD列的宏。

通过指定更多限制(例如进程运行的用户等),可以获得更准确的结果)。查看手册页以获取更多信息和其他开关。



例子:

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






使用shell



我不知道为什么要使用 .sh 脚本来运行你的代码,而不是直接调用 java ,但是如果你使用& (background)运算符,你可以通过你的shell获取 pid ,并使用 $!变量。



例如:

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

对于 java -jar ..&也是如此; 命令, $!将被设置为最后一次后台作业的pid。

Hello 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 ?

解决方案

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.

这篇关于在Linux下查找java进程的pid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:31