I have a quad core desktop, and i wanted to know the average temprature according to sensors. so i wrote this bash 1 liner.

echo `sensors -A | awk {'print $3'} | sed 's/+\|(crit\|0:\|°C//g'` |  awk '{print ($1 + $2 + $3 + $4)/4}'

but its not perfect I am sure. for example, if the number of cores changes, my script will break, or just not be as accurate. How might I write a script that looks at the number or output values, and adjusts for number of cores?

as in (pseudo code ahead):

echo `sensors -A | awk {'print $3'} | sed 's/+\|(crit\|0:\|°C//g'` |  awk '{print ($n + $n+1 <=($number of cores)) )/($number of cores)}'

I hope that is human readable. The output of the first part is something like:

$  echo `sensors -A | awk {'print $3'} | sed 's/+\|(crit\|0:\|°C//g'`
31.0 31.0 26.0 27.0

can i get some pro-tips on getting average cpu temperature?

  • 1
    First things first: Ditch the echo ``. It looks just plain ugly. – muru Apr 9 '15 at 16:38

You could do something like

sensors -A | grep -oP '^Core.+?  \+\K\d+' | awk '{k+=$1}END{print k/NR}'

The grep will print only the relevant numbers (the spaces ensure that only the actual temperature is printed, not the critical or anything else) and the awk does the calculation. NR is the number of lines so that will work if the number of cores changes.


Using the "raw output" mode of sensors for easier scripting:

-u    Raw output. This mode is suitable for debugging  and  for  post-
      processing  of  the  output  by  scripts. It is also useful when
      writing a configuration file because  it  shows  the  raw  input
      names which must be referenced in the configuration file.

For example:

$ sensors -Au
Physical id 0:
  temp1_input: 63.000
  temp1_max: 85.000
  temp1_crit: 105.000
  temp1_crit_alarm: 0.000
Core 0:
  temp2_input: 51.000
  temp2_max: 85.000
  temp2_crit: 105.000
  temp2_crit_alarm: 0.000

Armed with such nicely labeled fields, one can construct a far simpler awk command:

sensors -Au | awk '/temp.*_input/{temp += $2; count += 1} END {print temp/count}'

Essentially, for each temp.*_input field, add the temperature and increment a count, then at the end, print the total divided by the count.


You can get the number of processors using

grep -c ^processor /proc/cpuinfo
  • 1
    The nproc command could do that job. – muru Apr 9 '15 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.