Temperature Monitoring on Raspberry Pi Devices
Raspberry Pi boards come with a built-in temperature sensor, which can be accessed easily through the command line. Regular monitoring helps maintain system stability, especially during heavy workloads or extended usage. We’ll cover simple methods to check the CPU temperature using native tools like vcgencmd
and cat
, as well as how to set up automated monitoring to alert you when temperatures exceed safe thresholds.
Introduction
Monitoring the CPU temperature of Raspberry Pi 4 or any other Raspberry device is crucial for maintaining its performance and longevity, especially during demanding workloads or in warm environments. Overheating can lead to throttling, performance degradation, or even permanent hardware damage. By implementing a simple and efficient Bash script, you can monitor the CPU temperature in real time and take proactive measures to prevent overheating.
For instance, I observed that my Raspberry Pi 4 was reaching temperatures of around 70°C during the hottest part of the day. To address this, I decided to install a passive cooler and remove the top cover. These simple steps helped reduce the temperature by approximately 8-10°C.
In this article, we’ll guide you through the process of creating a Bash script to monitor the CPU temperature on your Raspberry Pi 4. Whether you’re running intensive applications or just curious about your device’s thermal health, this solution is straightforward to implement and highly customizable to meet your needs.
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