List of Java processes

前端 未结 18 2206
长发绾君心
长发绾君心 2020-12-22 16:44

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.

相关标签:
18条回答
  • 2020-12-22 17:22

    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
    
    0 讨论(0)
  • 2020-12-22 17:24

    There's a lot of ways of doing this. You can use java.lang.ProcessBuilder and "pgrep" to get the process id (PID) with something like: pgrep -fl java | awk {'print $1'}. Or, if you are running under Linux, you can query the /proc directory.

    I know, this seems horrible, and non portable, and even poorly implemented, I agree. But because Java actually runs in a VM, for some absurd reason that I can't really figure out after more then 15 years working the JDK, is why it isn't possible to see things outside the JVM space, it's really ridiculous with you think about it. You can do everything else, even fork and join child processes (those were an horrible way of multitasking when the world didn't know about threads or pthreads, what a hell! what's going in on with Java?! :).

    This will give an immense discussion I know, but anyways, there's a very good API that I already used in my projects and it's stable enough (it's OSS so you still need to stress test every version you use before really trusting the API): https://github.com/jezhumble/javasysmon

    JavaDoc: http://jezhumble.github.io/javasysmon/, search for the class com.jezhumble.javasysmon.OsProcess, she will do the trick. Hope it helped, best of luck.

    0 讨论(0)
  • 2020-12-22 17:25

    For better output format check this command:

    ps -fC java
    
    0 讨论(0)
  • 2020-12-22 17:25

    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.

    0 讨论(0)
  • 2020-12-22 17:26

    The following commands will return only Java ProcessIDs. These commands are very useful whenever you want to feed another process by this return values (java PIDs).

    sudo netstat -nlpt | awk '/java/ {print $7}' | tr '/java' ' '

    sudo netstat -nlpt | awk '/java/ {print $7}' | sed 's//java/ /g'

    But if you remove the latest pipe, you will notice these are java process

    sudo netstat -nlpt | awk '/java/ {print $7}'

    sudo netstat -nlpt | awk '/java/ {print $7}'

    0 讨论(0)
  • 2020-12-22 17:31

    I use this (good on Debian 8): alias psj='ps --no-headers -ww -C java -o pid,user,start_time,command'

    0 讨论(0)
提交回复
热议问题