List of Java processes

2019-01-29 22:43发布

How can I list all Java processes in bash? I need an command line. I know there is command ps but I don't know what parameters I need to use.

16条回答
Juvenile、少年°
2楼-- · 2019-01-29 22:56

Starting from Java 7, the simplest way and less error prone is to simply use the command jcmd that is part of the JDK such that it will work the same way on all OS.

Example:

> jcmd
5485 sun.tools.jcmd.JCmd
2125 MyProgram

jcmd allows to send diagnostic command requests to a running Java Virtual Machine (JVM).

More details about how to use jcmd.

See also the jcmd Utility

查看更多
混吃等死
3楼-- · 2019-01-29 22:56

If I want simply list java processes, use:

ps -A | grep java
查看更多
劫难
4楼-- · 2019-01-29 22:57

When I want to know if a certain Java class is getting executed, I use the following command line:

ps ww -f -C java | grep "fully.qualified.name.of.class"

From the OS side view, the process's command name is "java". The "ww" option widens the colum's maximum characters, so it's possible to grep the FQN of the related class.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-29 22:57

jps & jcmd wasn't showing me any results when I tried it using using openjdk-1.8 on redhat linux. But even if it did it only shows processes under the current user which doesn't work in my case. Using the ps|grep is what I ended up doing but the class path for some java apps can be extremely long which makes results illegible so I used sed to remove it. This is a bit rough still but removes everything except: PID, User, java-class/jar, args.

ps -o pid,user,cmd -C java | sed -e 's/\([0-9]\+ *[^ ]*\) *[^ ]* *\([^$]*\)/\1 \2/' -e 's/-c[^ ]* [^ ]* \|-[^ ]* //g'

Results look something like:

  PID USER     CMD
11251 userb org.apache.zookeeper.server.quorum.QuorumPeerMain ../config/zookeeper.properties
19574 userb com.intellij.idea.Main
28807 root org.apache.nifi.bootstrap.RunNiFi run
28829 root org.apache.nifi.NiFi

An alternative on windows to list all processes is:

WMIC path win32_process where "Caption='java.exe'" get ProcessId,Commandline

But that is going to need some parsing to make it more legible.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-29 23:01

This will return all the running java processes in linux environment. Then you can kill the process using the process ID.

ps -e|grep java
查看更多
可以哭但决不认输i
7楼-- · 2019-01-29 23:02

Recent Java comes with Java Virtual Machine Process Status Tool "jps"

http://download.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html

For example,

[nsushkin@fulton support]$ jps -m
2120 Main --userdir /home/nsushkin/.netbeans/7.0 --branding nb
26546 charles.jar
17600 Jps -m
查看更多
登录 后发表回答