Reputation: 55
I need to measure my CPU percentage usage and memory.
Using top command will give this result (see the red sign)
Another command by typing
top -d 1 | grep -w "CPU"
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
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
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