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
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
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: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 agoSomething 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.
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 -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}
Code: Select all
top -bn 2 -d 0.01 | grep '^%Cpu' | tail -n 1 | awk '{print $2+$4+$6}'
Code: Select all
CPU1: ${cpu cpu1}%
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 } '
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-completedebiman wrote:Code: Select all
grep '^cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {printf "%.2f%\n", usage}'
Code: Select all
awk '/^cpu/{usage=($2+$4)*100/($2+$4+$5)} END {printf "%.2f%\n", usage}' /proc/stat
I did see some variation, from 9.98% to 11,04%, while gkrellm peeked at 100%.debiman wrote:^ better.
however, the command doesn't work
just did a stress test, but it always shows 6.3%.
if i understand correctly, querying /proc/stat only once cannot show current cpu usage in percent - we need a delta between two reads.Head_on_a_Stick wrote:https://www.idnt.net/en-GB/kb/941772
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
Or would it be easier to use a simple top command?THX1138b wrote:Can anyone help me with a console command to get CPU usage?
I've been using the above script on one of my machines since seeing it in this thread. It certainly felt snappier and gave similar results to the previous script.Head_on_a_Stick wrote:https://www.idnt.net/en-GB/kb/941772
Code: Select all
5.9 MiB + 664.5 KiB = 6.5 MiB wmaker (2)
10.5 MiB + 1.4 MiB = 11.9 MiB wicd-client
12.6 MiB + 1.7 MiB = 14.2 MiB Xorg
15.0 MiB + 1.8 MiB = 16.8 MiB lilyterm
20.4 MiB + 175.0 KiB = 20.5 MiB cpu-usage.sh
50.6 MiB + 137.0 KiB = 50.7 MiB weechat-curses
---------------------------------
180.1 MiB
=================================
Code: Select all
└> $ uptime
10:24:24 up 18 days, 12:12, 3 users, load average: 0.61, 1.12, 1.38
┌—————————(kelsoo)————————<<<<(~) (Thu Sep 13 10:24:24)
└> $
Use conky, perhaps?oswaldkelso wrote:Any ideas?
you use the version from that web page, exactly, no changes?oswaldkelso wrote:I've been using the above script on one of my machines since seeing it in this thread. It certainly felt snappier and gave similar results to the previous script.Head_on_a_Stick wrote:https://www.idnt.net/en-GB/kb/941772
Today however I ran ps_mem I noticed after prolonged use it's memory usage had grown.
Code: Select all
5.9 MiB + 664.5 KiB = 6.5 MiB wmaker (2) 10.5 MiB + 1.4 MiB = 11.9 MiB wicd-client 12.6 MiB + 1.7 MiB = 14.2 MiB Xorg 15.0 MiB + 1.8 MiB = 16.8 MiB lilyterm 20.4 MiB + 175.0 KiB = 20.5 MiB cpu-usage.sh 50.6 MiB + 137.0 KiB = 50.7 MiB weechat-curses --------------------------------- 180.1 MiB =================================