24

我有一个在 Linux 计算机上运行的 Java 程序,并且想查找其进程的进程 ID (pid)。我知道该ps命令可以提供此信息,但它的输出令人困惑,因为它包含太多无关信息。我怎样才能得到 pid?


我在 Java 程序中使用 MPJ 库进行 Pagerank 算法。我编译它

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

并运行

mpjrun.sh -np 2 MpiPageRank

-np进程数在哪里

现在我必须找到它的 pid

ps -ef|grep java

喜欢

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

我明白了

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

现在我只想MpiPageRank从 1 个 linux comman 中提取它的 pid ie 27866。我怎么做 ?

4

5 回答 5

29

使用ps

ps-o允许用户使用开关为其输出定义自己的格式,并-C通过给定命令选择条目。我会去:

ps -C java -o pid

从手册页:

   -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.

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

例子:

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

使用外壳

我不知道为什么你使用.sh脚本来运行你的代码而不是java直接调用,但是如果你在任何情况下使用&(后台)操作符,你可以pid通过你的 shell来获取$!变量。

例如:

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

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

于 2012-04-18T02:02:57.510 回答
17

您可以使用 awk 获取 pid:

ps -ef | grep MpiPageRank | awk '{print $2}'

我注意到有时会找到 grep 本身,将其删除:

ps -ef | grep MpiPageRank | grep -v grep | awk '{print $2}'
于 2012-04-18T01:40:55.027 回答
12

jps 与 ps 相同,只是它只查看 java 进程。

如果您随后需要 PID,则可以执行以下操作:

jps | grep JAVA_NAME | awk '{print $1}'

运行 jps,然后使用 grep 按您要杀死的 java 应用程序或 jar 进行过滤。之后,awk 只捕获 pid 并将其打印到控制台。

于 2012-09-12T15:57:56.037 回答
8

一个简单的方法是运行这个:

pgrep java
于 2019-04-10T09:17:58.127 回答
0
ps -ef | grep java | grep MpiPageRank | grep -v grep | awk '{print $2}'

This will return exact pid of the MpiPageRank out of all java processes running   
ps -ef --> Returns all active processes
grep java --> Filter only java processes from all active processes
grep MpiPageRank --> Select only process related to MpiPageRank         
grep -v grep --> Remove the process related to Grep   
awk '{print $2}' --> select 2nd part of selected line
于 2020-04-30T05:12:24.107 回答