Skip to content

Instantly share code, notes, and snippets.

@squarefrog
Last active December 13, 2019 10:41
Show Gist options
  • Save squarefrog/09daf36988136ebe061601afe58f2df8 to your computer and use it in GitHub Desktop.
Save squarefrog/09daf36988136ebe061601afe58f2df8 to your computer and use it in GitHub Desktop.
Log CPU temperatures in Ubuntu
#!/bin/bash
#
# A script to log your CPU temps to a file.
# https://gist.github.com/squarefrog/09daf36988136ebe061601afe58f2df8
#
# You can set this script to run every X minutes using `cron`:
#
# $ sudo crontab -u username -e
#
# To run every 10 minutes, add the following line to your crontab:
#
# */10 * * * * /path/to/this/script/tempmon.sh
#
# Maximum number of log entries.
MAX_LOGS=52560 # 365 days at one reading every 10 minutes.
# Where to store the temperatures
FILENAME="/home/username/cpu-temp/results.tsv"
# Number of CPU cores
CORES=4
VALUES=$(date '+%Y-%m-%d %H:%M:%S')
for i in {1..CORES}
do
TEMP=`cat /sys/class/thermal/thermal_zone${i}/temp`
VALUE=$(bc <<< "scale=1; $TEMP/1000")
VALUES+="\t$VALUE"
done
if [[ -e "$FILENAME" ]]; then
FILE=`cat ${FILENAME}`
FILE+="\n"
fi
RESULT="$FILE$VALUES"
printf "$RESULT" | tail -n $MAX_LOGS > $FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment