Process Monitoring – How to Monitor Multiple PIDs with Top

bashmonitoringprocesspstop

I want to monitor memory usage for several processes and came up with a command like this:

ps aux |grep -e postgres -e unicorn -e nginx|cut -d' ' -f2|for i in $(xargs); do echo $i; done

16112
16113
...

How can I change the bit after the last pipe to feed arguments into top -p $i, so I get an of overall idea of memory consumption for all pids? The final command would produce something like top -p<pid1> -p<pid2> and so on

Best Answer

How about something like

pids=( $(pgrep 'postgres|unicorn|nginx') )

to put the PIDs in an array, and then

top "${pids[@]/#/-p }"

to spit them back out into top, prepending each with -p

Related Question