Skip to content

Instantly share code, notes, and snippets.

@devlavender
Last active February 3, 2022 14:09
Show Gist options
  • Save devlavender/0fa63d78eecc7b2398b4778d3ede6b7e to your computer and use it in GitHub Desktop.
Save devlavender/0fa63d78eecc7b2398b4778d3ede6b7e to your computer and use it in GitHub Desktop.
Colorful Cli Temperature Checker for CPU/GPU
#!/bin/bash
#########################################################
# viewsensors.sh #
# #
# - Author: #
# Bruno Moreira-Guedes <brunodOut.dev@gmail.com> #
# - Date: 2021-11-28 #
# - Description: #
# Small script to view CPU/GPU temperature in #
# colors. I just wrote it because I wanted to see #
# it well in a transparent terminal window on #
# alacritty / wayfire. I'm not the best with #
# colors, so feel free to define better ones. #
# #
#########################################################
TMPFILE="/tmp/sensors"
TAB_SIZE=9
LOG_DST="/dev/null"
tabs $TAB_SIZE
relabel(){
sed "s/$1/$2/"
}
sensors_read(){
sensors coretemp-isa-0000 \
nouveau-pci-0100 >"$TMPFILE"
}
s_log(){
cat >> $LOG_DST
# Never mind those log lines, I was just using
# it for debug purposes
}
file_read(){
cat $TMPFILE
}
process_input(){
relabel "temp1:" "GPU:" |
relabel "Package id 0:" "Coreavg:" |
grep -e '\+[[:digit:][:punct:]]\{1,\}C' |
cut -d'(' -f1 | sed 's/[[:blank:]]//g' |
grep -v '^[[:space:]]*$'
}
# Likely no longer necessary:
# hdr_unspace(){
# sed \
# 's/\t\(\[[:alnum:]]\{1,\}\)\([[:space:]]\{1,\}\)\([[:alnum:]]\{1,\}\)\t/\1\3/g'
# }
get_col(){
# From when it was get_ln:
# sed -n "$1"p
cut -d':' -f"$1"
}
fillspace(){
n="$1"
cur=0
spc=""
while [ "$cur" -lt "$n" ]; do
cur=$(($cur + 1))
spc="$spc "
done
echo "$spc"
}
tk_centralize(){
token=$(cat)
tk_sz=${#token}
size=$(($TAB_SIZE - 1))
if [ "$1" != "" ]; then
size="$1"
fi
echo "tk_centralize" \
" size=$size tk_sz=$tk_sz" \
" token='$token':" | s_log
offset=$(bc <<< "($size/2) - ($tk_sz/2)")
echo "offset=$offset" | s_log
end=$(($offset))
echo "end=$end" | s_log
tk_ret=$(fillspace "$end")
echo "tk_centralize size=$size tk_ret='$tk_ret'" |
s_log
tk_ret="$token""$tk_ret"
echo "tk_centralize size=$size " \
"tk_ret='$tk_ret'" | s_log
begin=$(($size - ${#tk_ret}))
echo "begin=$begin " \
"fillspace='$(fillspace $begin)'" |
s_log
tk_ret=$(fillspace "$begin")"$tk_ret"
echo "$tk_ret"
echo "tk_ret='$tk_ret'" | s_log
}
field_tkformat(){
ret=""
sep=""
ctr=0
while read nx_tk; do
ret="$ret""$sep"$(echo "$nx_tk" | \
tk_centralize)
ctr=$(($ctr + 1))
sep="\t"
done
COLUMNS_LAST_TKLINE="$ctr"
echo "$ret"
}
colorify(){
GREP_COLOR="$1" grep -E \
--color=always "^.*$"
}
color_chars(){
colormult=$1
count=0
shift 1
t_s="a"
colorized_a=$(echo $t_s | \
GREP_COLOR="$*" grep --color=always a)
echo $((${#colorized_a} - 1))
# Temporarily removed:
#for i in $(echo $* | tr ';' '\n'); do
# echo $i | \
# grep -E '^[[:digit:]]{1,3}$' \
# 2>&1 >/dev/null && \
# count=$(($count + $colormult))
#done
#echo "$count"
}
process_column(){
col_n="$1"
colors="$2"
echo "process_column col_n='$col_n' " \
"colors='$colors' cols=$(tput cols) " \
"color_chars=$(color_chars 10)" | s_log
process_input | get_col "$col_n" |
field_tkformat | colorify "$2" | tk_centralize \
$((`tput cols` + $(color_chars 10 "$colors")))
}
sensors_read
HEADER=$(file_read | process_column 1 '01;02;04;31;40')
DATA=$(file_read | process_column 2 '01;02;33;40')
echo -e "$HEADER"
echo -e "$DATA"
# Kept for reference (of how this started)
#tabs 9 && sensors coretemp-isa-0000 nouveau-pci-0100|sed 's/temp1:/ GPU: /'|sed 's/Package\ id\ 0:/Coreavg: /' |grep -e '\+[0-9\.]*°C\s'|cut -d'(' -f1|sed 's/[[:blank:]]\{1,\}/\ /g'|datamash transpose -t ':'|sed 's/\ *:\ */:/g'|sed 's/^\ //'|sed 's/:/\t/g'|sed 's/\(Core [0-9]\)/ \1/g'|sed 's/GPU/ GPU /'|GREP_COLOR='01;02;04;31;40' grep -E --color=always "^|^Core.*$"|GREP_COLOR='01;02;33;40' grep -E --color=auto "^|[[:punct:][:digit:]]{1,}C.*"
tabs 8
#!/bin/sh
#########################################################
# watchsensors.sh #
# #
# - Author: #
# Bruno Moreira-Guedes <brunodOut.dev@gmail.com> #
# - Date: 2021-11-28 #
# - Description: #
# Small script to watch the execution of the #
# viewsensors.sh script. This one I wrote because #
# watch was messing up with viewsensors.sh colors #
# and I could not find a reasonable solution. #
# Yeah, it's not ideal, don't tell me, but it was #
# very fun to write it!! #
# #
#########################################################
interval=0.5
ex_key='q'
last_key=''
if [ "$1" != "" ]; then
interval="$1"
fi
if [ "$2" != "" ]; then
ex_key="$2"
fi
tput smcup
get_ln() {
sed -n "$1"p
}
write_header(){
tput cup 0 0
tabs 4
echo -e "Watching Sensors\tItval: $interval\t"\
"Exit Key: $ex_key\t`hostname`: `date`"
}
write_footer(){
tput cup $(tput lines) 0
echo -n "Press '$ex_key' to exit"
}
write_cont_header(){
tput cup 2 0
viewsensors.sh | get_ln 1
}
write_cont_main(){
tput cup 3 0
viewsensors.sh | get_ln 2
}
write_content() {
write_cont_header
write_cont_main
}
read_key(){
read -n 1 -t 0.5 -s last_key
}
display_all() {
tput init
write_header
sleep 0.01
write_content
write_footer
}
display_all
trap display_all WINCH
tput civis
while [ "$last_key" != "$ex_key" ]; do
write_header
write_content
read_key
done
tput cnorm
#tput cup $(tput lines) 0
tput rmcup

Colurful Cli Temperature Monitor for Linux

It's a couple of bash scripts I have written just to monitor the CPUs+GPU temperature in my computer. It might need some tweaks to work on yours, like changing sensor names and adapt the processing of output format.

I'm just horrible at picking colors, so you'll likely need to change them if you want it to look good for you.

Dependencies

You'll need lm_sensors and datamash installed on your system.

Screenshot

The following screenshot illustrates the concept (never mind my terrible taste for design). image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment