Colored CPU usage in TMUX

Those spending a lot of time in Linux command line know the value of a good terminal multiplexer. Even in the age of GUI terminal windows, using tmux will speed your work while many customizations it offers will make for comfortable environment.

Knowing its flexibility, for my Minecraft server, I wanted tmux status line to include something I never tried before - CPU usage. Yes, one could use top, htop, glances, or any other of the many monitoring tools but I didn't want the whole screen occupied by it. I wanted just a small unobtrusive note in the corner.

There are multitude of ways to retrieve CPU usage but I found vmstat works well for me and thus the first version just placed its parsed output in the bottom-right corner:

~/.tmux.conf
set-option -g status-interval 1
set-window-option -g status-right ' #( vmstat 1 2 | tail -1 | awk "{ printf 100-\$15; }" )% '

While this satisfied my original requirements, I started thinking about colors. Could I make it more colorful?

Search immediately found me a few tmux plugins and one of them looked really promising. While using it would be perfectly fine for my use case, I don't like dependencies and thus I tried to find tmux-only solution for colors.

A bit of testing later and I noticed shell commands (enclosed in #()) are processed before formatting directives. So, if I manage to return the formatting from shell, tmux will do further processing and colors will be there. A bit of awk-ing later and this is what I came up with.

~/.tmux.conf
set-option -g status-interval 1
set-window-option -g status-right ' #( vmstat 1 2 | tail -1 | awk "{ USAGE=100-\$15; if (USAGE < 20) { printf \"#[fg=green,bright]\"; } else if (USAGE < 80) { printf \"#[fg=yellow,bright]\"; } else { printf \"#[bg=red,fg=white,bright]\"; }; print \" \" USAGE \"% \" }" )'

It's still vmstat based output but now awk appends color formatting strings for different usage levels. If usage is lower than 20%, a bright green foreground is used; lower than 80% results in a bright yellow text; and anything higher results in a bright red background.

It's a simple color-coded way of showing CPU usage at a glance.


PS: If you want extra status line text after CPU percentage, consider adding #[bg=default,fg=default] to stop color "bleeding" to remainder of the line.

4 thoughts to “Colored CPU usage in TMUX”

    1. That would be something like this

      set-window-option -g status-right ' #( free -m | grep "^Mem" | awk "{print \$4}" ) MB #( vmstat 1 2 | tail -1 | awk "{ USAGE=100-\$15; if (USAGE<20) { printf \"#[fg=colour2,none]\"; } else if (USAGE<80) { printf \"#[fg=colour3,none]\"; } else { printf \"#[bg=red]#[fg=colour15,none]\"; }; print \" \" USAGE \"% \" }" )'
      

      Essentially it's just adding output of free -m | grep "^Mem" | awk "{print \$4}"

  1. Hi,
    Thanks for the Script. Works great.
    I Used CPU uasage, But after enabling this awesome script my clock disapeared. can you add STH to bring the clock back.
    i would like to learn my self how to do that.
    if any refrence will be apritiated.
    Thank you so much

  2. Hossein Derakhandeh:

    set-window-option -g status-right ‘CPU:#( vmstat 1 2 | tail -1 | awk “{ USAGE=100-\$15; if (USAGE < 20) { printf \"#[fg=green,bright]\"; } else if (USAGE < 80) { printf \"#[fg=yellow,bright]\"; } else { printf \"#[bg=red,fg=white,bright]\"; }; print \" \" USAGE \"% \" }" ) #[bg=default,fg=default]%H:%M'

Leave a Reply to Josip Medved Cancel reply

Your email address will not be published. Required fields are marked *