Raspberry Pi Temperature Script

There is no default way to check the Pi’s temperature. I have found a few blogs where they explain how to create a script to check the temperature but failed to find one that is easy to follow and straight forward. This is why I am going to show you how to create a Raspberry Pi temperature script as well create a command to execute it as standard linux command.

Now I am no expert script writer. I have mostly followed two blog posts and combined the best parts, credits are here.

Creating the script

We will create a simple bash script. Copy the following command and paste it in the terminal. This should create the “temp.sh” script and open in edit mode.

nano temp.sh

Copy the following code and paste it in the script file.

#!/bin/bash
# Script: temp.sh
# Purpose: Display the ARM CPU and GPU temperature of Raspberry Pi
# Author: Ahmed Redwan <www.bytegeeks.net> under GPL v3.0
# -------------------------------------------------------
cpu=$(</sys/class/thermal/thermal_zone0/temp)
c=$(bc -l <<< "scale=1; ($cpu)/1000")
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $(/opt/vc/bin/vcgencmd measure_temp)"
echo "CPU => temp=$c'C"

Now save and exit the file (Ctrl + S, then Ctrl + X). Once this is done, we need to make the script as executable. The following command is used to make it executable.

chmod +x temp.sh

To run any executable script we have to append “./” to the name of the script

./temp.sh

You should see something like this.

output of temp script in Raspberry Pi

Now we need to copy this script to /usr/bin/ directory to be able to execute the bash script like regular linux commands as shown in the second temperature output.

sudo cp temp.sh /usr/bin/temp

Once we have copied it to the directory, typing only “temp” should run the script and output the CPU and GPU temperature of your Raspberry Pi.

Credits

Vivek Gite and Shahriar Shovon

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *