Andika Trisna Adhi
Andika Trisna Adhi

Reputation: 55

Grep CPU and other hardware percentage using top command?

I need to measure my CPU percentage usage and memory.
Using top command will give this result (see the red sign) cpu info 1

Another command by typing

top -d 1 | grep -w "CPU"

The result is like this. CPU INFO 2

How do i grab the percent value of cpu usage to a variable in bash linux?
Need help everyone :)
Many thanks

Upvotes: 1

Views: 9173

Answers (2)

OrangesV
OrangesV

Reputation: 322

I think using mpstat rather than top is much easier to parse information regarding processor.

mpstat -P ALL 1 1

-P to indicate processor `ALL` for all of them
and shows 1 interval of 1 second

Upvotes: 1

anubhava
anubhava

Reputation: 786359

To get the CPU percentage value you can use top -n1 -b command and pipe it to awk:

top -n 1 -b | awk '/^%Cpu/{print $2}'

Upvotes: 4

Related Questions