Using the approach mentioned in the answer by Rick Byers:
top -p `pgrep java | paste -sd "," -`
but I had more than 20 processes running so following command can be helpful for someone who encounter a similar situation.
top -p `pgrep java | head -n 20 | paste -sd "," -`
pgrep
gets the list of processes with given name - java in this case. head
is used to get first 20 pids because top cannot handle more than 20 pids when using -p argument. Finally paste
joins the list of pids with ','.
You can control the process name you are looking for in the above command and the number of processes with that name you are interested to watch. You can ignore the head -n 20
part if the number of your processes with the given name is less than 20.