Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Monday, 16 June 2014

Automatically Check The Raspberry Pi's Temperature (+Prevent Overheating)

No need to install anything. Just open a terminal and execute the following command to get the temperature of your RasPi.
/opt/vc/bin/vcgencmd measure_temp

Automate the process

The below script will automatically reboot your Raspberry Pi should it reach a specified threshold (maxtemp). First of all make sure you can reboot/shutdown your raspi without linux asking you for a password. 

user@user:~$ sudo visudo
Add the following to the sudoers file. 
user ALL=NOPASSWD: /sbin/shutdown

The script

Save as temp.sh. Maxtemp specifies the temperature cut-off point, at which the Raspi will reboot. 
#!/bin/bash  
#title           :temp.sh  
#description     :Checks Temperature of Raspberry Pi and reboots at maxtemp.  
#author          :Mike M | www.linuxx.eu  
#notes           :Add "user ALL=NOPASSWD: /sbin/shutdown" to sudoers file   
  
temp=`/opt/vc/bin/vcgencmd measure_temp | cut -c 6-7`  
maxtemp=75  
if [ $temp -ge $maxtemp ]; then  
xmessage  "Alert! Temp above 75. System rebooting in 3 minutes"  
sudo /sbin/shutdown -r +3  
elif [ $temp -lt $maxtemp ]; then  
exit 0  
fi

Create a cronjob

user@user:~$ sudo crontab

Add the following

PATH=/usr/sbin:/usr/bin:/sbin:/bin
*/5 * * * * sh /home/user/temp.sh
This will run the temp.sh script every 5 minutes and make sure that the Raspberry Pi does not overheat.