Created
April 30, 2021 20:15
-
-
Save austonpramodh/a45dcf13dc085de3128a575353de2d11 to your computer and use it in GitHub Desktop.
Raspberry pi 4 CPU Temp and Frequency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #---------------------------------------------------------------------------------------- | |
| # Obtains the current value of the CPU temperature. | |
| #---------------------------------------------------------------------------------------- | |
| function get_cpu_temp() | |
| { | |
| # Initialize the result. | |
| local result=0.00 | |
| # The first line in this file holds the CPU temperature as an integer times 1000. | |
| # Read the first line from the file. | |
| line=$(head -n 1 /sys/class/thermal/thermal_zone0/temp) | |
| # Test if the string is an integer as expected with a regular expression. | |
| if [[ $line =~ ^-?[0-9]+$ ]] | |
| then | |
| # Convert the CPU temperature to degrees Celsius and store as a string. | |
| result=$(awk "BEGIN {printf \"%.2f\n\", $line/1000}") | |
| fi | |
| # Give the result back to the caller. | |
| echo "$result" | |
| } | |
| #---------------------------------------------------------------------------------------- | |
| # Obtains the current value of the CPU frequency. | |
| #---------------------------------------------------------------------------------------- | |
| function get_cpu_freq() | |
| { | |
| # Initialize the result. | |
| local result=0.00 | |
| # The first line in this file holds the CPU temperature as an integer times 1000. | |
| # Read the first line from the file. | |
| line=$(head -n 1 /sys/devices/system/cpu/cpu$1/cpufreq/scaling_cur_freq) | |
| # Test if the string is an integer as expected with a regular expression. | |
| if [[ $line =~ ^-?[0-9]+$ ]] | |
| then | |
| # Convert the CPU temperature to degrees Celsius and store as a string. | |
| result=$(awk "BEGIN {printf \"%.2f\n\", $line/1000}") | |
| fi | |
| # Give the result back to the caller. | |
| echo "$result" | |
| } | |
| #---------------------------------------------------------------------------------------- | |
| # Program to demonstrate how to obtain the current value of the CPU temperature. | |
| #---------------------------------------------------------------------------------------- | |
| cputemp=$(get_cpu_temp) | |
| echo -e "Current CPU temperature is $cputemp degrees Celsius." | |
| cpufreq0=$(get_cpu_freq 0) | |
| echo -e "Current CPU frequency for core 0 is $cpufreq0 Mhz." | |
| cpufreq1=$(get_cpu_freq 1) | |
| echo -e "Current CPU frequency for core 0 is $cpufreq1 Mhz." | |
| cpufreq2=$(get_cpu_freq 2) | |
| echo -e "Current CPU frequency for core 0 is $cpufreq2 Mhz." | |
| cpufreq3=$(get_cpu_freq 3) | |
| echo -e "Current CPU frequency for core 0 is $cpufreq3 Mhz." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment