Terminal command to get CPU usage?

If it doesn't relate to Debian, but you still want to share it, please do it here

Terminal command to get CPU usage?

Postby THX1138b » 2018-08-23 14:52

Can anyone help me with a console command to get CPU usage? On my Raspberry Pi running Raspbian Stretch I get output like this:
Code: Select all
$ iostat -c
Linux 4.14.34-v7+ (raspberry)    23/08/18    _armv7l_   (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           2.62    0.00    0.30    0.14    0.00   96.93
from iostat but a) this doesn't match what the CPU usage monitor on the taskbar says (right now it's ~70%) and b) I need just a single number (e.g. 70.21 or whatever). Thanks.
antiX 16, Wyse V90L thin client with VIA Eden 32-bit 800 MHz CPU and 1 GB 533 MHz DDR2
Raspberry Pi 3 B and 3 B+, both running Raspbian Stretch

We don't inherit the Earth from our parents; we borrow it from our children.
THX1138b
 
Posts: 35
Joined: 2017-09-12 20:57

Re: Terminal command to get CPU usage?

Postby arochester » 2018-08-23 15:12

arochester
 
Posts: 1312
Joined: 2010-12-07 19:55

Re: Terminal command to get CPU usage?

Postby THX1138b » 2018-08-23 19:34

Won't commands that work on Debian also work on Raspbian?
antiX 16, Wyse V90L thin client with VIA Eden 32-bit 800 MHz CPU and 1 GB 533 MHz DDR2
Raspberry Pi 3 B and 3 B+, both running Raspbian Stretch

We don't inherit the Earth from our parents; we borrow it from our children.
THX1138b
 
Posts: 35
Joined: 2017-09-12 20:57

Re: Terminal command to get CPU usage?

Postby arochester » 2018-08-23 20:28

Not necessarily.

As I have said before, start with Debian, add something, take something away, change something. It is no longer Debian, it is something else.

As oswaldkelso said some time ago
Something to be aware of: Debian is a core or source distribution. This means there are many Debian-based distributions. THEY ARE NOT DEBIAN. Their information may or may not be useful or safely applied to Debian. Debian has no way of knowing what has been changed on these systems.
arochester
 
Posts: 1312
Joined: 2010-12-07 19:55

Re: Terminal command to get CPU usage?

Postby THX1138b » 2018-08-23 20:43

arochester wrote:Not necessarily.

As I have said before, start with Debian, add something, take something away, change something. It is no longer Debian, it is something else.

As oswaldkelso said some time ago
Something to be aware of: Debian is a core or source distribution. This means there are many Debian-based distributions. THEY ARE NOT DEBIAN. Their information may or may not be useful or safely applied to Debian. Debian has no way of knowing what has been changed on these systems.


Okay, well, this command works in case anyone finds their way here through a search engine in the future:
Code: Select all
top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}
antiX 16, Wyse V90L thin client with VIA Eden 32-bit 800 MHz CPU and 1 GB 533 MHz DDR2
Raspberry Pi 3 B and 3 B+, both running Raspbian Stretch

We don't inherit the Earth from our parents; we borrow it from our children.
THX1138b
 
Posts: 35
Joined: 2017-09-12 20:57

Re: Terminal command to get CPU usage?

Postby sunrat » 2018-08-24 00:29

THX1138b wrote:
arochester wrote:Okay, well, this command works in case anyone finds their way here through a search engine in the future:
Code: Select all
top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | gawk '{print $2+$4+$6}

That command doesn't work here. Just missing the last character -
Code: Select all
top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | awk '{print $2+$4+$6}'


CPU usage changes every cycle which is probably in the nanoseconds. You can only measure a snapshot of it at the time of measuring. It also has a bit of a quantum effect in that it will be different just because you are measuring it.
I have a Conky which shows it every second and it's never the same 2 seconds in a row unless it's working hard and maxed out. Inside .conkyrc it's just a variable
Code: Select all
CPU1: ${cpu cpu1}%
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2429
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: Terminal command to get CPU usage?

Postby oswaldkelso » 2018-08-24 15:00

Try this. It's what I have running in my tint2 panel and works on my Dragora system and did on my Debian systems. :twisted:

Code: Select all
top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' -v prefix="$prefix" '{ split($1, vs, ","); v=vs[length(vs)]; sub("%", "", v); printf "CPU:" "%s%.1f%%\n", prefix, 100 - v } '


Good quote that and I stand by it... not sure if it really applies to top, tail and grep though :mrgreen:
Ash init durbatulûk, ash init gimbatul,
Ash init thrakatulûk agh burzum-ishi krimpatul.
User avatar
oswaldkelso
 
Posts: 1136
Joined: 2005-07-26 23:20
Location: UK

Re: Terminal command to get CPU usage?

Postby debiman » 2018-08-26 08:49

^ god, that's ugly.
so many pipes!
i like the averaging though.

i found this online:
Code: Select all
grep '^cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {printf "%.2f%\n", usage}'

advantages: the stats are queried directly with grep, and besides awk no other utilities are required to process the output.
User avatar
debiman
 
Posts: 2818
Joined: 2013-03-12 07:18

Re: Terminal command to get CPU usage?

Postby Head_on_a_Stick » 2018-08-26 08:51

debiman wrote:
Code: Select all
grep '^cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {printf "%.2f%\n", usage}'

awk can do searches as well, it is Turing-complete ;)
Code: Select all
awk '/^cpu/{usage=($2+$4)*100/($2+$4+$5)} END {printf "%.2f%\n", usage}' /proc/stat

Bloody interweb... :x
User avatar
Head_on_a_Stick
 
Posts: 7904
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Terminal command to get CPU usage?

Postby debiman » 2018-08-26 09:46

^ better.

however, the command doesn't work :-(
just did a stress test, but it always shows 6.3%.
User avatar
debiman
 
Posts: 2818
Joined: 2013-03-12 07:18

Re: Terminal command to get CPU usage?

Postby arzgi » 2018-08-26 10:08

debiman wrote:^ better.

however, the command doesn't work :-(
just did a stress test, but it always shows 6.3%.


I did see some variation, from 9.98% to 11,04%, while gkrellm peeked at 100%.
arzgi
 
Posts: 337
Joined: 2008-02-21 17:03
Location: Finland


Re: Terminal command to get CPU usage?

Postby debiman » 2018-08-26 13:04

Head_on_a_Stick wrote:https://www.idnt.net/en-GB/kb/941772

if i understand correctly, querying /proc/stat only once cannot show current cpu usage in percent - we need a delta between two reads.
User avatar
debiman
 
Posts: 2818
Joined: 2013-03-12 07:18

Re: Terminal command to get CPU usage?

Postby Head_on_a_Stick » 2018-08-26 13:59

^ Looks like it, yes. You could have a peek at top(1)'s code and see how it generates the readout:

http://procps.sourceforge.net/
User avatar
Head_on_a_Stick
 
Posts: 7904
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Terminal command to get CPU usage?

Postby debiman » 2018-08-28 04:58

oh, that bash script is nice enough.
it does the delta, but still no averaging.

i was hacking on it on the weekend, just for fun:

Code: Select all
#!/bin/bash

delay=1

tput civis
trap 'tput cnorm' EXIT

# sleep as a builtin
for file in /usr/lib/bash/sleep /usr/lib32/bash/sleep /usr/lib64/bash/sleep; do
    [ -r "$file" ] && enable -f "$file" sleep && break
done
# Portable enough?

while :; do
  # Get the first line with aggregate of all CPUs
  read -r -a cpu_now </proc/stat
  # Get all columns but skip the first (which is the "cpu" string)
  cpu_sum="${cpu_now[@]:1}"
  # Replace the column seperator (space) with +
  cpu_sum="${cpu_sum// /+}"
  # Get the delta between two reads
  cpu_delta=$((cpu_sum - cpu_last_sum))
  # Get the idle time Delta
  cpu_idle=$((cpu_now[4]- cpu_last[4]))
  # Calc time spent working
  cpu_used=$((cpu_delta - cpu_idle))
  # Calc percentage
  cpu_usage=$((100 * cpu_used / cpu_delta))

  # Keep this as last for our next read
  cpu_last=("${cpu_now[@]}")
  cpu_last_sum=$cpu_sum

  case $cpu_usage in
    [1-4]?|?) color='' ;;
    [5-7]?) color='\e[33m' ;; # yellow
    [8-9]?|100) color='\e[31m' ;; # red
  esac

  printf "\r\E[0KCPU ${color}%02d%%\E[0m" $cpu_usage
 
  # Wait a second before the next read
  sleep "$delay"
done

maybe i can get the averaging in at some point, maybe even make it POSIX compliant (sh instead of bash, no arrays).
User avatar
debiman
 
Posts: 2818
Joined: 2013-03-12 07:18

Next

Return to Offtopic

Who is online

Users browsing this forum: No registered users and 1 guest

fashionable