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.
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- JQ: Select when attribute value exists in a bash a
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
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
andjoin
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.is most useful. Prints just pid and qualified main class name:
The above will
(btw, this example is not the effective one, but simple to remember) ;)
you can pipe the above to another commands, for example:
etc...