Skip to content

Instantly share code, notes, and snippets.

@RandomArray
Forked from uGeek/INSTALL.md
Last active January 7, 2022 06:38
Show Gist options
  • Save RandomArray/f142e7f5284c4d3727fec6c30f84edc0 to your computer and use it in GitHub Desktop.
Save RandomArray/f142e7f5284c4d3727fec6c30f84edc0 to your computer and use it in GitHub Desktop.
Log Raspberry Pi temperature to text file
#!/bin/bash
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))
#RPi4 has a different path to vcgencmd on bullseye
gpuTemp=$(/opt/vc/bin/vcgencmd measure_temp 2>/dev/null || /usr/bin/vcgencmd measure_temp 2>/dev/null)
# Function to write the temperature into the log
function writeToLog() {
# Path to log file
file="/home/pi/temp.log"
# Check if the file exists
if [ ! -f "$file" ] ; then
# if not create the file
touch "$file"
fi
echo "$1" >> "$file"
}
# Save the value with ISO 8601 timestamp
writeToLog "$(date --iso-8601=seconds) - CPU Temp: $cpuTemp1.$cpuTempM'C - GPU Temp: ${gpuTemp:5}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment