Here's the only solution so far for MacOS:
top -pid `pgrep java | awk 'ORS=" -pid "' | sed 's/.\{6\}$//'`
though this will undesirably report invalid option or syntax: -pid
if there are no java
processes alive.
EXPLANATION
The other solutions posted here use the format top -p id1,id2,id3
, but MacOS' top
only supports the unwieldy format top -pid id1 -pid id2 -pid id3
.
So firstly, we obtain the list of process ids which have process name "java":
pgrep java
and we pipe this to awk
which joins the results with delimitor " -pid "
| awk 'ORS=" -pid "'
Alas, this leaves a trailing delimitor! For example, we may so far have obtained the string "123 -pid 456 -pid 789 -pid "
.
We then just use sed
to shave off the final 6 characters of the delimitor.
| sed 's/.\{6\}$//'`
We're ready to pass the results to top
:
top -pid `...`