Search code examples
xfce

Linux CPU and memory percentages


Is there a command which outputs just current CPU usage percentage and current memory usage percentage? As a single number, so no tables or formatted output.

The reason I'm asking. For my panel in XFCE I'd like to see something like this:

CPU 34% | MEM 56%

I haven't found a plugin which does that, so I aim to use the Generic Monitor plugin and give it a command which it should print and let it update every 1 sec.


Solution

  • Put the following snippet somewhere in a script:

    #!/bin/bash
    
    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%
    

    And call it from genmon as bash /path/to/this/script.sh.