Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Bash script that prints the current state of the CPU on a Raspberry Pi. It displays variables like temperature, voltage and speed.
#!/bin/bash
# cpustatus
#
# Prints the current state of the CPU like temperature, voltage and speed.
# The temperature is reported in degrees Celsius (C) while
# the CPU speed is calculated in megahertz (MHz).
function convert_to_MHz {
let value=$1/1000
echo "$value"
}
function calculate_overvolts {
# We can safely ignore the integer
# part of the decimal argument
# since it's not realistic to run the Pi
# at voltages higher than 1.99 V
let overvolts=${1#*.}-20
echo "$overvolts"
}
temp=$(vcgencmd measure_temp)
temp=${temp:5:4}
volts=$(vcgencmd measure_volts)
volts=${volts:5:4}
if [ $volts != "1.20" ]; then
overvolts=$(calculate_overvolts $volts)
fi
minFreq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq)
minFreq=$(convert_to_MHz $minFreq)
maxFreq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq)
maxFreq=$(convert_to_MHz $maxFreq)
freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
freq=$(convert_to_MHz $freq)
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
echo "Temperature: $temp C"
echo -n "Voltage: $volts V"
[ $overvolts ] && echo " (+0.$overvolts overvolt)" || echo -e "\r"
echo "Min speed: $minFreq MHz"
echo "Max speed: $maxFreq MHz"
echo "Current speed: $freq MHz"
echo "Governor: $governor"
exit 0
@jmac-slash0

This comment has been minimized.

Show comment
Hide comment
@jmac-slash0

jmac-slash0 Jul 5, 2013

I had to use POSIX syntax to get this to work in OpenElec 3.0.6:

convert_to_MHz () {
let value=$1/1000
echo "$value"
}

calculate_overvolts () {
let overvolts=${1#*.}-20
echo "$overvolts"
}

Handy script though, thanks.

I had to use POSIX syntax to get this to work in OpenElec 3.0.6:

convert_to_MHz () {
let value=$1/1000
echo "$value"
}

calculate_overvolts () {
let overvolts=${1#*.}-20
echo "$overvolts"
}

Handy script though, thanks.

@ecampidoglio

This comment has been minimized.

Show comment
Hide comment
@ecampidoglio

ecampidoglio Jul 5, 2013

Cool. Thanks for sharing :)

Owner

ecampidoglio commented Jul 5, 2013

Cool. Thanks for sharing :)

@Vedrillan

This comment has been minimized.

Show comment
Hide comment
@Vedrillan

Vedrillan Jul 29, 2013

To get it working on my openelec 3.0.6 I had to change function declaration like this

convert_to_MHz (){
    let value=$1/1000
    echo "$value"
}

calculate_overvolts () {
    # We can safely ignore the integer
    # part of the decimal argument
    # since it's not realistic to run the Pi
    # at voltages higher than 1.99 V
    let overvolts=${1#*.}-20
    echo "$overvolts"
}

Otherwise I get this

openelec:~ # bash -x cpustatus
+ function convert_to_MHz {
cpustatus: line 1: function: not found
+ let value=/1000
cpustatus: let: line 1: arithmetic syntax error
+ echo

cpustatus: line 11: syntax error: unexpected "}"

EDIT:
I just realized I said the same thing as Fatal-Optimism xD

To get it working on my openelec 3.0.6 I had to change function declaration like this

convert_to_MHz (){
    let value=$1/1000
    echo "$value"
}

calculate_overvolts () {
    # We can safely ignore the integer
    # part of the decimal argument
    # since it's not realistic to run the Pi
    # at voltages higher than 1.99 V
    let overvolts=${1#*.}-20
    echo "$overvolts"
}

Otherwise I get this

openelec:~ # bash -x cpustatus
+ function convert_to_MHz {
cpustatus: line 1: function: not found
+ let value=/1000
cpustatus: let: line 1: arithmetic syntax error
+ echo

cpustatus: line 11: syntax error: unexpected "}"

EDIT:
I just realized I said the same thing as Fatal-Optimism xD

@moskk

This comment has been minimized.

Show comment
Hide comment
@moskk

moskk Aug 1, 2015

on ArchLinuxARM vcgencmd executable located in /opt/vc/bin, which is not in PATH by default. i put these lines in .bashrc:

PATH=$PATH:/opt/vc/bin
export PATH

and then it works properly.

moskk commented Aug 1, 2015

on ArchLinuxARM vcgencmd executable located in /opt/vc/bin, which is not in PATH by default. i put these lines in .bashrc:

PATH=$PATH:/opt/vc/bin
export PATH

and then it works properly.

@mcgyver83

This comment has been minimized.

Show comment
Hide comment
@mcgyver83

mcgyver83 Aug 31, 2016

On a raspberry pi2 I have this error executing this script:
cpustatus.sh: 23: cpustatus.sh: Bad substitution

On a raspberry pi2 I have this error executing this script:
cpustatus.sh: 23: cpustatus.sh: Bad substitution

@ThomasKaiser

This comment has been minimized.

Show comment
Hide comment
@ThomasKaiser

ThomasKaiser Apr 8, 2017

/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq is wrong unfortunately! Don't trust in it if you're interested in CPU clock. This is controlled by the 'firmware', the kernel has no clue at which frequency the CPU cores are running and you need to query the firmware with vcgencmd measure_clock arm instead especially if you're interested in overclocking.

bamarni/pi64#4 (comment)

/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq is wrong unfortunately! Don't trust in it if you're interested in CPU clock. This is controlled by the 'firmware', the kernel has no clue at which frequency the CPU cores are running and you need to query the firmware with vcgencmd measure_clock arm instead especially if you're interested in overclocking.

bamarni/pi64#4 (comment)

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