Shell How to get script-readable, human-readable CPU usage for i3bar?



Messages: 24


This fine forum has been very helpful through my FreeBSD journey, but one thing is bugging me. For Linux I used i3pystatus to manage my i3 status bar, for my configs see here, but as soon as I tried it on FreeBSD it became apparent it wouldn't work for it, as it relied on /proc/* and other bits of info specific to Linux (to provide RAM, CPU usage and core temperatures).

So presently I'm working on a i3status shell script, so far the only three bits of information I've managed to get into it are RAM usage (as a fraction of used/total available, obtained it via freecolor), uptime and date + time. How do I get CPU usage as a percentage average over all four cores of my PC? I'd also like this percentage to incorporate both system and user usage of it, not just one or the other. I plan on migrating to an eight core PC soon so an easily portable answer would be much appreciated. I know that vmstat and iostat express CPU usage for user and system, but I want the overall usage, not just or the other. Is this possible? Not sure if simply adding the two would be appropriate, like using GNU Octave to add the two from iostat output, e.g. here's the output for iostat:

       tty            ada0              da0              da1             cpu
tin  tout  KB/t tps  MB/s   KB/t tps  MB/s   KB/t tps  MB/s  us ni sy in id
   8   101 31.37  17  0.53   2.57   0  0.00   2.44   0  0.00  16  0 17  0 67
maybe if I add the numbers under us and sy I might get the total usage? Is it that simple, I ask because something tells me it may not be.
If anyone here uses i3 under FreeBSD I'd love to check our your configs, as whenever I search for i3 configs in DuckDuckGo/Google/GitHub I get Linux configs, which aren't always applicable to FreeBSD.

Thanks for your time and patience


Staff member

Reaction score: 8,236
Messages: 31,984

You can add the numbers for us (user), ni (niced), sy (system) and in (interrupt). Basically everything except id (idle). They're the same numbers you see at the top of top(1). The overall CPU usage is all four numbers combined.

CPU 0:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle