Linux下查找java进程的pid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10201589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:50:56  来源:igfitidea点击:

Find the pid of a java process under Linux

javalinuxgreppidps

提问by Shweta B. Patil

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

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

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

and run by

并运行

mpjrun.sh -np 2 MpiPageRank

where -npis number of process

-np进程数在哪里

Now i have to find its pid

现在我必须找到它的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 MpiPageRankfrom only 1 linux comman to get its pid ie 27866. how do i do that ?

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

回答by Abdullah Jibaly

You can use awk to get the pid:

您可以使用 awk 来获取 pid:

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

I noticed that sometimes grep itself gets found, to remove it:

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

ps -ef | grep MpiPageRank | grep -v grep | awk '{print }'

回答by c00kiemon5ter

using ps

使用 ps

psallows the user to define his own formatting for its output with the -oswitch, and -Cto select entries by the given command. I would go with:

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

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 .shscript to run your code and not call javadirectly, but if in any case you use the &(background) operator, you can grab the pidthrough your shell, with the $!variable.

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

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.

这同样适用于java -jar .. &命令,$!将被设置为最后转到后台作业的PID。

回答by Jesan Fafon

jps is the same as ps except it only looks at java processes.

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

If you then need the PID, you can do something like the following:

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

jps | grep JAVA_NAME | awk '{print }'

That runs jps, then uses grep to filter by the java application or jar you want to kill. After that, awk captures and prints just the pid to the console.

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

回答by Eusuf Kanchwala

A simple way is to run this:

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

pgrep java

回答by pratap

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

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

这将返回所有运行的 Java 进程中 MpiPageRank 的确切 pid