Skip to content

Instantly share code, notes, and snippets.

@refo
Last active July 1, 2020 14:49
Show Gist options
  • Select an option

  • Save refo/dac4cc21e9f2aaa8c12c0c6386ceb599 to your computer and use it in GitHub Desktop.

Select an option

Save refo/dac4cc21e9f2aaa8c12c0c6386ceb599 to your computer and use it in GitHub Desktop.
Print raspberry pi gpu and cpu temperatures in json format

Installation

Paste the script to /usr/bin/temp and make it executable

# copy the contents of "raspberry_print_temp_json.sh"
sudo nano /usr/bin/temp
# paste
# ctrl + o
# enter
# ctrl + x

# make it executable
sudo chmod +x /usr/bin/temp

Example outputs

pi@raspberrypi:~ $ temp
{
  "cpu": 50.0,
  "gpu": 50.5,
  "date": "Wed Jul  1 15:42:41 BST 2020",
  "hostname": "raspberrypi"
}

pi@raspberrypi:~ $ temp -l
{ "cpu": 50.0, "gpu": 50.5, "date": "Wed Jul  1 15:42:44 BST 2020", "hostname": "raspberrypi"}
cpu=$(</sys/class/thermal/thermal_zone0/temp)
cpu=$((cpu/1000))
gpu=$(/opt/vc/bin/vcgencmd measure_temp)
gpu=${gpu//[temp=|\'C]/}
ind="\n "
nl="\n"
while getopts 'l' flag; do
case "${flag}" in
l) ind=" "
nl="" ;;
*) ;;
esac
done
fmt="{$ind\"cpu\": %.1f,$ind\"gpu\": %.1f,$ind\"date\": \"$(date)\",$ind\"hostname\": \"$(hostname)\"$nl}"
printf "$fmt\n" \
$cpu $gpu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment