What is wrong with this script for my xfce panel?


This is the script that i found: (https://stackoverflow.com/questions/52354782/linux-cpu-and-memory-percentages/70685230#70685230)


CPU=$(lscpu | grep '\(CPU\|max\) MHz:' | xargs echo | awk '{printf "%3.0f\n", $3*100/$7}')
MEM=$(free | grep Mem | awk '{printf "%3.0f\n", $3*100/$2}')
echo CPU $CPU% \| MEM $MEM%

Have you checked the result of each command?


Try now:


CPU=$(top -bn1 | grep load | awk '{printf "%.2f%\n", $(NF-2)}')
MEM=$(free -t | awk 'NR == 2 {printf "%.2f%\n", $3/$2*100}')
echo CPU $CPU \| MEM $MEM

Also make sure the script is executable:

chmod +x my_script.sh

This does not provide any useful output.

1 Like

I havent tought about this.
I will. Thanks

I tried, now it indicates the cpu value, but its 1-3%. Not really real data.

But i really appreciate your work!

Maybe this then:


CPU=$(top -bn1 | grep "Cpu(s)" | \
           sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
           awk '{print 100 - $1"%"}')
MEM=$(free -t | awk 'NR == 2 {printf "%.2f%\n", $3/$2*100}')
echo CPU $CPU \| MEM $MEM

Man, i’m really thankful, but it seems like dosn’t work either:

although I still don’t fully understand that sed line
and could never have come up with it myself:
there should be a comma after id (the third line of the “top” output)

the above is not a quote anymore as I already changed that :wink:

… and it only works reliably if I set LANG=C as well …

ps (a few hours later):
the original script does work when setting LANG=C in the calling terminal or in the script
So: it is probably not the comma / point after id in the sed line
(which I do not understand)
but something else

comma or point makes no difference - but the LANG setting does
it ensures consistency
different LANG settings produce slightly different output
some use decimal point, some comma - and the first column varies between %Cpu(s): and %CPU(s):
so that the grep command returns empty handed in one case
tricky :wink: