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

10

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 Commented Mar 7, 2015 at 18:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.