BASH script that prints hardware’s temperatures

Here is a simple BASH script I wrote, that can print out the temperatures of:

  • CPUs.
  • GPU.
  • Disks.
  • TINs sensors.

You can tweak it as you like and as needed in order to suit your Linux system.

To run it, you’ll have to be root, or use `sudo`. Since it relies on `smartctl` which can not be executed by normal users. Unless you change the group that the /sbin/smartctl belongs to, to one of the groups you’re member of. Or adding extra ACL to that file.

And here is the source code:

#!/bin/bash
# ------------------------------------------------------------
#  Script:    systemps
# License:    GPL3
#      By:    Mubarak Alrashidi (DeaDSouL)
#  Social:    t: @_DeaDSouL_  G+: +MubarakAlrashidi
#     URL:    http://unix.cafe
# ------------------------------------------------------------
CPUS=2  # total number of CPU(s) NOT cores
TINS=(SYSTIN CPUTIN AUXTIN0 AUXTIN1 AUXTIN2 AUXTIN3)
# ------------------------------------------------------------

sensors=`sensors`

# CPU(s)
for ((i=0; i<"${CPUS}";i++)); do
        temp_cpu=`echo "${sensors}" | egrep "^Physical id ${i}" | awk '{print $4}' | cut -c '2,3'`
        echo "  CPU ${i}: ${temp_cpu}.0°C"
done; echo ''

# nVidia GPU 0
temp_gpu0=`nvidia-smi -q -d TEMPERATURE | grep 'GPU Current Temp' | awk '{print $5}'`
echo "  GPU 0: ${temp_gpu0}.0°C"; echo ''

# Disks
for i in $(lsblk | egrep ^sd | awk '{print $1}'); do
        echo "    ${i}: $(smartctl -A /dev/${i} | egrep '^194|^190' | awk '{print $10}').0°C"
done; echo ''

# SYSTIN CPUTIN AUXTIN0 AUXTIN1 AUXTIN2 AUXTIN3
for i in "${TINS[@]}"; do s=''
        temp_tin=`echo "${sensors}" | egrep "^${i}" | awk '{print $2}' | tr -d '+'`
        [[ "$i" == 'SYSTIN' || "$i" == 'CPUTIN' ]] && s=' '
        echo "${s}${i}: ${temp_tin}"
done

Yet, it can alternatively be downloaded from here: SysTemps_script.tar.gz

And.. Happy Linuxing 🙂

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.