Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active June 11, 2020 07:30
Show Gist options
  • Save cellularmitosis/03b632b530c22ab21772 to your computer and use it in GitHub Desktop.
Save cellularmitosis/03b632b530c22ab21772 to your computer and use it in GitHub Desktop.
Bash functions to read bogomips and cpu mhz from /proc/cpuinfo

Blog 2015/10/2

<- previous | index | next ->

iscpuslow.sh: Bash function to read bogomips and MHz from /proc/cpuinfo

I had a need to run a script but have different behavior on older machines vs. newer machines, and wrote this Bash function to figure out if a machine is "slow".

#!/bin/bash
set -e
set -o pipefail
set -x
# functions:
function has_bogomips() { grep --quiet -i 'bogomips' /proc/cpuinfo ; }
function bogomips() { grep -i 'bogomips' /proc/cpuinfo | tr ':.' ' ' | awk '{print $2}' ; }
function has_mhz() { grep --quiet -i 'cpu mhz' /proc/cpuinfo ; }
function mhz() { grep -i 'cpu mhz' /proc/cpuinfo | tr ':.' ' ' | awk '{print $2}' ; }
function cpu_is_slow()
{
if has_bogomips && [ "$(bogomips)" -lt 1000 ] ; then return 0 ; fi
if has_mhz && [ "$(mhz)" -lt 500 ] ; then return 0 ; fi
return 1
}
# usage:
if cpu_is_slow
then
echo "slow!"
else
echo "not slow."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment