Results 1 to 4 of 4

Thread: BASH Total CPU Percentage

  1. #1
    Join Date
    Feb 2011
    Location
    South Africa
    Beans
    52

    Smile BASH Total CPU Percentage

    Hi,

    I've been searching for a neat way of finding the total instantaneous CPU usage as a %, and found all sorts of tedious solutions.

    I thought I'd share a simple one-liner that accomplishes the task really simply:

    Code:
     top -bn 1 | awk '{print $9}' | tail -n +8 | awk '{s+=$1} END {print s}'
    Basically, it grabs the CPU column from top and sums all the values together. Simple as that.

    Enjoy!
    eklikeroomys,
    Another Ubuntu fanatic.

  2. #2
    Join Date
    Feb 2008
    Beans
    Hidden!

    Re: BASH Total CPU Percentage

    very slick.


  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,243
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: BASH Total CPU Percentage

    this will do the same
    Code:
    top -bn 1 | awk 'NR>7{s+=$9} END {print s}'
    imho that code shows wrong number, you have to divide by number of cores.
    in my case system monitor shows approx: 20/20/10/10
    code: 60
    Code:
    top -bn 1 | awk 'NR>7{s+=$9} END {print s/4}'
    why not print the 3rd line where 'ingredients' of CPU load are listed?
    Code:
    top -bn 1 | sed -n 3p
    or assuming cpu load = 100% - idle%
    Code:
    top -bn 1 | awk 'BEGIN{FS="[ \t%]+"} NR==3{ print 100-$8 }'
    Last edited by Vaphell; November 16th, 2012 at 04:18 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Feb 2011
    Location
    South Africa
    Beans
    52

    Re: BASH Total CPU Percentage

    I agree that dividing by the number of CPUs will give the actual percentage of total CPU usage if you have more than one core. Thanks!
    eklikeroomys,
    Another Ubuntu fanatic.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •