ng-User
ng-User

Reputation: 243

bash | How to get value of %MEM from TOP command

enter image description here

How can I grep easily the value of %MEM only from TOP of specific user (e.g tomcat) on console:

top -u tomcat | grep ????

expected output:

0.1

Upvotes: 1

Views: 508

Answers (1)

James Risner
James Risner

Reputation: 6104

How can I grep easily the value of %MEM only from TOP of specific user (e.g tomcat) on console?

This just shows the %MEM values:

top -b -n 1 -u tomcat -o %MEM | awk \
    'BEGIN {p=0} /%MEM/ {p=1; getline} {if (p) print $10}'

This runs top with -b (batch / background / script mode), only once (-n 1), for user tomcat, order by memory. Pass that information to awk to discard all before %MEM (handled by the p=0 -> 1 change).

This produces a list like:

0.2
0.1
0.1
0.0

If you just wish the top value, add this after the awk:

| head -1

If you wish to total it, change awk to:

'BEGIN {p=0} /%MEM/ {p=1; getline} {if (p) t+=$10} END {print t}'

Upvotes: 0

Related Questions