3

I just bought a heat sink kit for my pi and I'm using Raspbian and would like to monitor the temperature continuously every 3 seconds via SSH while I perform tasks on the pi so that I don't have to keep manually entering in a command to display the current temperature, how do I do that?

3 Answers 3

9

I think using watch command is much easier, for example:

watch {your command}

In your case, it will be:

watch vcgencmd measure_temp

That will refresh by 2 seconds, if you want to define the interval by yourself (5 second maybe), you can type:

watch -n 5 <your command>

In your case, it will be:

watch -n 5 vcgencmd measure_temp

You can cancel that with Ctrl+C.

2
  • much more elegant and simple, thanks for the response
    – bsara
    Jan 3, 2017 at 16:47
  • the use of watch is new to me. very elegant.
    – tswaehn
    Sep 26, 2020 at 10:46
2

The command for viewing the current temperature is found in this post: https://raspberrypi.stackexchange.com/a/1847/22221

To automate this such that you can view the temperature every 3 seconds without entering new commands can be accomplished by writing a simple shell script that has an infinite loop:

#!/bin/bash

echo "Press [ctrl+c] to end monitoring"
echo ""

while true
do
  vcgencmd measure_temp
  sleep 3s
done

Place that text in a file called monitor-temp and place it in ~/bin/ (if !/bin/ doesn't already exist, then just create it with mkdir ~/bin). Make monitor-temp executable: chmod 755 ~/bin/monitor-temp. End your terminal (or ssh) session and start a new one. Now you can run monitor-temp from any location in the file system via ssh or directly on the machine and you'll see something like the following:

pi@raspberrypi ~ $ monitor-temp
Press [ctrl+c] to end monitoring

temp=48.7'C
temp=48.7'C
temp=49.2'C
temp=49.2'C
temp=48.7'C
1

If you want a few more bells as whistles, see the following script:

#!/bin/bash

for count in $(seq 1 100); do
    echo -n "$(vcgencmd measure_temp | echo "$(sed -E 's/.*([0-9][0-9]\.[0-9]).*/\1/g')" | echo "$(xargs)*1.8" | bc | echo "$(xargs)+32" | bc | echo "$(xargs)'F / $(vcgencmd measure_temp | sed -E 's/.*([0-9][0-9]\.[0-9]).*/\1/g')'C")\r"
    sleep 3s
done

The main benefit of this script is that the line carriage is removed so that you don't overflow your terminal window with new lines. Additionally, the temperature in F and C is displayed.

Sample output:

94.1'F / 35.0'C

This will terminate after 100 checks but can easily be modified to run indefinitely if preferred.

I created the following alias for this script in my ~/.bash_profile:

alias temp="sh temp_script.sh"

NOTE: The removed carriages returns only works when executing via "sh" command (i.e. executing script via ./temp_script.sh does not respect the \r special character for some reason). Also, there is some redundancy in the echo command, but I made the tradeoff of having it be a one liner. Lastly, Fahrenheit conversion is a bit wonky and doesn't match up perfectly, although the input formula should be correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.