Skip to content

Instantly share code, notes, and snippets.

@grantjr1842
Forked from geerlingguy/pi-cpu-stress.sh
Created July 15, 2025 02:40
Show Gist options
  • Select an option

  • Save grantjr1842/c21060d5c631ce92976525dac811adc3 to your computer and use it in GitHub Desktop.

Select an option

Save grantjr1842/c21060d5c631ce92976525dac811adc3 to your computer and use it in GitHub Desktop.
Raspberry Pi CPU temperature and throttling test script
#!/bin/bash
# Raspberry Pi stress CPU temperature measurement script.
#
# Download this script (e.g. with wget) and give it execute permissions (chmod +x).
# Then run it with ./pi-cpu-stress.sh
#
# NOTE: In recent years, I've switched to using s-tui. See:
# https://github.com/amanusk/s-tui?tab=readme-ov-file#options
# Variables.
test_run=1
test_results_file="${HOME}/cpu_temp_$test_run.csv"
stress_length="10m"
# Verify stress-ng is installed.
if ! [ -x "$(command -v stress-ng)" ]; then
printf "Error: stress-ng not installed.\n"
printf "To install: sudo apt install -y stress-ng\n" >&2
exit 1
fi
printf "Logging temperature and throttling data to: $test_results_file\n"
# Start logging temperature data in the background.
while /bin/true; do
line=$(date | tr '\n' '\t')
# For Raspberry Pi.
line+=$(vcgencmd measure_temp | tr -d "temp=" | tr -d "'C" | tr '\n' '\t')
line+=$(vcgencmd get_throttled | tr -d "throttled=" | tr '\n' '\t')
line+=$(vcgencmd measure_clock arm | sed 's/^.*=//')
# For Intel/AMD/Ampere (might need tweaking).
# line+=$(sensors -A | sed -n '2{s/°.*//; s/[^+-]*//; p; q}' | tr '\n' '\t')
# line+=$(echo 'Not_Measured' | tr '\n' '\t')
# line+=$(awk '/MHz/{ temp+=$4; n++ } END{ printf("%f\n", temp/n) }' /proc/cpuinfo | tr '\n' '\t')
echo $line
echo $line >> $test_results_file
sleep 5
done &
# Store the logging pid.
PROC_ID=$!
# Stop the logging loop if script is interrupted or when it ends.
trap "kill $PROC_ID" EXIT
# After 5 minutes, run stress.
printf "Waiting 5 minutes for stable idle temperature...\n"
printf "Date °C Status CPU Clock\n"
sleep 300
printf "Beginning $stress_length stress test...\n"
stress-ng -c 4 --timeout $stress_length
# Keep logging for 5 more minutes.
printf "Waiting 5 minutes to return to idle temperature...\n"
sleep 300
printf "Test complete.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment