I wrote a script for my dwm
statusbar. One part of it is finding the current cpu usage for every single core on my system. I figured out a way myself but I would need some help fixing a bug in this. Here is the command: top -bn 2 -d 0.5 | grep -F '%Cpu' | tail -n 4 | awk '{print $3}'
. This assumes that your top
shows every core per default. You can achieve this by issuing top
then pressing 1
and then pressing W
to save the current configuration to a .toprc
file in your home folder. Everytime you open top
now it will display all of your cores. My aforementioned command has the drawback that when assigned to a variable like this:
CPU=$(top -bn 2 -d 0.5 | grep -F '%Cpu' | tail -n 4 | awk '{print $3}')
and then using $CPU
with xsetroot
like this: xsetroot -name "$CPU"
I will get the output I want in my statusbar but between every cpu percentage there will be two symbols on top of each other separating them v
and t
. How do I get rid of them? Has this something to do that I might be using an array instead of a string here?
You can see the problem on the left side of the picture.
Command:
top -bn 2 -d 0.5 | grep -F '%Cpu' | tail -n 4 | awk '{print $3}'
sample output:
14.3
12.0
8.0
10.0
Note for anyone using this script: When the cpu usage for a core reaches 100%
the array that the command outputs will move the column with the current load from column 3
to column 2
. Hence, with awk '{print $3}'
you will then see us,
as output for column 3
. If you're fine with that leave it. If not you could have awk
print column 2
as well. It will just be :
. A solution that avoids all those pitfalls is:
top -bn 2 | grep -F '%Cpu' | tail -n 4 | gawk '{print $2 $3}' | tr -s '\n\:\,[:alpha:]' ' '
top -bn 2 -d 0.5
.Archlinux
with kernel version3.16.1-1-ARCH
;x86-64
. You really don't wanna see the ouput oftop -bn 2 -d 0.5
that will be an aweful long list of running processes.../proc/stat
to get the same information more cheaply. Seeproc(5)
.