Raspberry Pi4 CPU temperature monitoring with bash script
I had an issue that during operation, my Raspberry Pi4 had a CPU temperature higher than 60 degrees Celsius. I wanted to check in which period of the day this happens, and I developed a script that periodically saved information about the temperature in a log file.
Bash script
To check periodically temperature on my Raspberry Pi4 on Ubuntu 22.04 I developed the following script:
#!/bin/bash
LOG_FILE="/opt/scripts/log/check_temp_$(date +%Y-%m-%d).log" # Location for log file
echo "Check temperature script is executed on $(date)" >> $LOG_FILE
# Loop indefinitely
while :
do
temp=$(sensors| grep "temp1" | awk '{print $2}')
echo "$(date): Temp: $temp" >> $LOG_FILE
sleep 600
# Check if the date has changed
if [ "$(date +%Y-%m-%d)" != "$(date -r "$LOG_FILE" +%Y-%m-%d)" ]; then
# Create a new log file for the new day
LOG_FILE="/opt/scripts/log/check_temp_$(date +%Y-%m-%d).log"
echo "Script is executed on $(date)" >> "$LOG_FILE"
fi
done
You can save the script on the following script:
nano /opt/scripts/check_temp.sh
To make it executable:
chmod +x /opt/scripts/check_temp.sh
Add the script in the Crontab
To run it after every reboot, automatically add the following line in the cron:
# Check temp
@reboot sleep 180 && sudo bash /opt/scripts/check_temp.sh