Search code examples
bashgrepash

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


Solution

  • 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}'