LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   command line cpu utilization (https://www.linuxquestions.org/questions/linux-software-2/command-line-cpu-utilization-803989/)

ninja master 04-24-2010 04:15 PM

command line cpu utilization
 
how would one take the mpstat command, and have the idle time subtracted from 100.... then dump said number to a file or preferably print it to command line?

i want my cpu utilization in non robot time and in non robot /proc/loadavg

thats right i want total cpu utilization % printed in bash (why doesnt nix have this in the proc folder?) (why doesnt mp stat have a flag to show total cpu usage and not give me the run around with % kernel is using % user is using % sys % nice??????)

i asked in another thread but they told me to forward x on an xless system. i can see several ways to display cpu utilization but none answering the question and google totally gives the same answer they gave in the other thread.

sar is broken on this machine, and it is non existent on the other.

sar is now broken on the linux from scratch machine also, and mpstat is running strong on it also =)

with this question, i would like to add for SMP users such as myself.... how would one do that same % but instead of system total, %'s run on individual cores.

much thanks LQ, and im one of the top folders on the folding@home team now =)

ninja master 04-24-2010 04:41 PM

Code:

#!/bin/bash
# by Paul Colby (http://colby.id.au), no rights reserved ;)
# modified by a drunken penguin to take a simple snapshot of cpu usage

PREV_TOTAL=0
PREV_IDLE=0

while true; do
  CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics.
  unset CPU[0]                          # Discard the "cpu" prefix.
  IDLE=${CPU[4]}                        # Get the idle CPU time.

  # Calculate the total CPU time.
  TOTAL=0
  for VALUE in "${CPU[@]}"; do
    let "TOTAL=$TOTAL+$VALUE"
  done

  # Calculate the CPU usage since we last checked.
  let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
  echo "CPU: $DIFF_USAGE%"
exit

i rephrased the problem, and found a bash script....

it was giving constant updates and printing oddly so i modified it to suit my purpose


All times are GMT -5. The time now is 11:28 AM.