I want to collect total RSS used by all foo
processes and pass it to quux
script as an argument.
function collect_foo {
local X=0
local P
local M
for P in $(pgrep foo)
do
M=$(cat /proc/$P/statm | awk '{ print ($2 * 4096) }')
X=$[X+M]
done
echo $X
}
quux $(collect_foo)
cat
can accept multiple arguments. Is it possible to construct all /proc/$P/statm
paths without an explicit loop? For example, by using pgrep -d , foo I can make a comma-separated PID list suitable for {}
. Then the output can be passed to awk that sums it up using BEGIN/END
blocks.
I'm stuck at {}
. Can something like cat /proc/{$(pgrep -d , foo)}/statm
be done? {}
doesn't expand.
I know that in general summing up RSS is not a good idea, the question is about indirect shell expansion. Any other means of shorten the code are welcome though.
quux $(eval cat /proc/{$(pgrep -d , foo)}/statm | awk '{sum += $2}END{ print (sum * 4096)}')
– nponeccop Jun 4 '14 at 8:52