Skip to content

Instantly share code, notes, and snippets.

@angelperezleon
Forked from geerlingguy/pi-cpu-stress.sh
Last active February 3, 2024 13:19
Show Gist options
  • Save angelperezleon/669aa61330a3a353186480932fc8a075 to your computer and use it in GitHub Desktop.
Save angelperezleon/669aa61330a3a353186480932fc8a075 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
# Variables
#time stamp the log file
now=$(date +"%d-%m-%Y-%H%M%S")
test_results_file="${HOME}/cpu_temp_$now.log"
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
# Print the date (e.g. "Wed 13 Nov 18:24:45 GMT 2019") and a tab.
date | tr '\n' '\t' >> $test_results_file;
# Print the temperature (e.g. "39.0") and a tab.
vcgencmd measure_temp | tr -d "temp=" | tr -d "'C" | tr '\n' '\t' >> $test_results_file;
# Print the throttle status (e.g. "0x0") and a tab.
vcgencmd get_throttled | tr -d "throttled=" | tr '\n' '\t' >> $test_results_file;
# Print the current CPU frequency.
vcgencmd measure_clock arm | sed 's/^.*=//' >> $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"
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 "Average temperature recorded was: \n"
# calculate number of rows then devide to get average temp.
awk '{sum= sum+$7} END {print sum/NR}' $test_results_file;
printf "Test complete.\n"
@angelperezleon
Copy link
Author

Made my own changes, just simple date stamps onto log file and calculate the average temp on each run and print this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment