5

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

1 Answer 1

8

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

1
  • 1
    very short and clever technique, this will prove useful for me in many scenarios Mar 7, 2015 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .