Track CPU and GPU temperature in tmux

Ever since I work with my own remote computer, I worry about my hardware not to overheat. Especially when training a NN, I used to monitor the GPU temperature, in an active tmux panel running with ​nvidia-smi. Whe I first saw people using different plugins to see the CPU load on the tmux status bar, I realized I was wasting too much space on that panel, and looked for a GPU-temp solution. Here is a compilation of my findings.

The different options

tmux-gpu is a good gpu monitor for tmux which is inspired from rainbarf . However, I am looking for number representation of the CPU and GPU temperature, and no graphs.

Powerline is a unified statusbar for many applications, including tmux and vim, which is based on python. Its installation for tmux requires a couple of steps which are different depending on which your remote and local computer OSs are. There are a plethora of plugins for powerline, like this one. However, even though I found powerline powerful, I don’t want to have it installed just for a temperature monitor. I wanted a more simple, lightweight solution.

tmux-cpu

There is a great tmux-plugin called tmux-cpu which only monitors the cpu and gpu load. It’s lightweight and can be easily installed with either the Tmux Plugin Manager (tpm) or directly. Even though it does not covered, it looks like it can be easily modified to include temperature monitoring as well.

You can read more about the installation of tpm and tmux-cpu in their respective sites, but in short, once having tpm installed, you only need to include in ~/.tmux.conf:

set -g @plugin 'tmux-plugins/tmux-cpu'

and then press prefix + I (CAPS) to load the new plugin.

Editing tmux-cpu to show temperatures

First, make sure the commands for obtaining your hardware temperature are working. For Ubuntu Linux with a Nvidia graphics card, I use sensors and nvidia-smi.

To get sensors I installed lm-sensors and configured them using these instructions. For nvidia-smi, of course you need to install the nvidia-drivers.

Then, add these two scripts in ~/.tmux/plugins/tmux-cpu/scripts/:

cpu_temp.sh:

#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

source "$CURRENT_DIR/helpers.sh"

print_cpu_temp() {
if command_exists "sensors"; then

sensors | sed '/^[^Package]/d' | sed '/^\s*$/d' | tail -n 1 | awk '{a=$4} END {printf("%5.0fºC", a)}'

else
echo "no sensors found"
fi
}

main() {
print_cpu_temp
}
main

gpu_temp.sh:

#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

source "$CURRENT_DIR/helpers.sh"

print_gpu_temp() {
if command_exists "nvidia-smi"; then
loads=$(nvidia-smi | sed -nr 's/.*\s([0-9]+)C.*/\1/p')
gpus=$(echo "$loads" | wc -l)
load=$(echo "$loads" | awk '{count+=$1} END {print count}')
echo "$load" | awk '{printf "%3.0fºC", $1}'
else
echo "nvidia-smi not found"
fi
}

main() {
print_gpu_temp
}
main

Then edit both cpu_interpolation and cpu_commands in ​~/.tmux/plugins/tmux-cpu/cpu.tmux:

cpu_interpolation=(
"\#{cpu_percentage}"
"\#{cpu_icon}"
"\#{cpu_bg_color}"
"\#{cpu_fg_color}"
"\#{gpu_percentage}"
"\#{gpu_icon}"
"\#{gpu_bg_color}"
"\#{gpu_fg_color}"
"\#{cpu_temp}"
"\#{gpu_temp}"
)


cpu_commands=(
"#($CURRENT_DIR/scripts/cpu_percentage.sh)"
"#($CURRENT_DIR/scripts/cpu_icon.sh)"
"#($CURRENT_DIR/scripts/cpu_bg_color.sh)"
"#($CURRENT_DIR/scripts/cpu_fg_color.sh)"
"#($CURRENT_DIR/scripts/gpu_percentage.sh)"
"#($CURRENT_DIR/scripts/gpu_icon.sh)"
"#($CURRENT_DIR/scripts/gpu_bg_color.sh)"
"#($CURRENT_DIR/scripts/gpu_fg_color.sh)"
"#($CURRENT_DIR/scripts/cpu_temp.sh)"
"#($CURRENT_DIR/scripts/gpu_temp.sh)"
)

Finally, add this line to .tmux.conf:

#Configure tmux-cpu
set -g status-right '| CPU: #{cpu_percentage}#{cpu_temp} GPU:#{gpu_percentage}#{gpu_temp} | %h-%d %H:%M '

Then with prefix+I, the tmux bar should look look this:

Screenshot 2019-02-06 17.30.25

It seems like there is a lot of much more fancy stuff with tmux status bars, but for now this is what I just need.


Originally published at HELLO, I AM CRISTIAN DUGUET.