Varun Paliwal Varun Paliwal - 6 months ago 12
Bash Question

How to top a list of pids from a file in linux?

I've a script to get a list of pid's
I want to see cpu usage, memory usage of those pids using top.

I am able to top a list of pid's using:

top -p pid1,pid2,pid3


I have a function which returns the pid's I need, which I am converting into a comma separated list as follows:

$ gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g'
12775,13319,14404,14549,14920,27534


However, the following isn't working:

gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g'| top -p


It gives the error:

top: -p argument missing


Need some help to get this working - i.e. to be able to top the pid's returned by my function above.

Thanks

Answer

You are using | (pipe) and it connects stdout of one process to stdin of the other.

What you need is

top -p $(your_function_here)

for example

top -p $(gal vppoc|cut -d" " -f1|xargs|sed -e 's/ /,/g')