If you call the top command, you get all the running processes. But how can I limit the output only to a certain process name like "java"?
I've tried this top -l 2 | grep java but in this way you get only snapshots and not a continuously updated list. And top -l 0 | grep java is not really clear.
Running the below will give continuous update in console:
Use the watch command
A more specific case, like I actually was looking for:
For Java processes you can also use
jps -q
whereby jps is a tool from $JAVA_HOME/bin and hence should be in your $PATH.I came here looking for the answer to this on OSX. I ended up getting what I wanted with bash and awk:
I loop top in logging mode and filter it with awk, building an associative array from the output of pgrep. Awk prints the first 12 lines, where line 12 is the column headers, and then every line which has a pid that's a key in the array. The dump file is used for a more watchable loop.
just
top -bn 1 | grep java
will do the trick for youI run it (eg.):
top -b | egrep -w 'java|mysqld'