Graphing CPU Temperatures in Linux

I recently changed motherboards and as always mounting the fan and heatsink on the CPU makes me feel stupid. I have a large non-stock cooler and it is always a struggle to make it fit with all the other things motherboard makers stuff near the CPU these days. After some busted knuckles, I thought I had it. But the temperature was a little high for my taste and seemed to climb for no good reason.

I looked at the setup and realized the plate that touches the CPU was off center. So I decided to remount it. Well, turns out there was a reason the plate wouldn’t center, but I eventually solved it. But all this has given me an unsual interest in temperature the last few days.

I won’t tell you how to set up lmsensors since there are plenty of good references on how to do so (a quick google on “lmsensors howto” shows lots of pages including . I even used ksensor (I use KDE) to give a nice display. I was thinking how nice it would be to automatically collect the data and graph it. Then I thought, well, why not?

I wrote a quick and dirty script — you’d need to change it for your setup. I have an AMD 5600+ (overclocked though), so when I run “sensors” I get several devices. The one I wanted was the k8temp-pci-00c3 which shows the two core temperatures. Unfortunately, it isn’t in a great format for processing:

k8temp-pci-00c3
Adapter: PCI adapter
Core0 Temp:  +38.0°C
Core1 Temp:  +36.0°C

But certainly awk is up to the challenge of fixing this up. Here’s the quicky script:

#!/bin/bash
#usage: tempdata delay file
echo Press ^C to exit
echo "Date/Time,Core 0,Core 1">$2
while true
do
   echo -n `date +%Y%m%d%H%M%S,` >>$2
   sensors k8temp-pci-00c3 | awk 'NR==3 { gsub/.C/,""); c1=$3; } NR==4 { gsub(/.C/,""); print c1 "," $3; } >>$2
   sleep $1
done
[/caption]

So there you have it. A classic example of using little Linux tools to create something bigger.

4 thoughts on “Graphing CPU Temperatures in Linux

  1. In my machine k8temp-pci-00c3 senses my PCIe graphics card, CPU temps are read by a diferent sensor (it8716-isa-0228 in my case).

  2. I am not familiar with BASH. I did replace “k8temp-pci-00c3 senses” with what I got when I ran sensors (“k10temp-pci-00c3″) but when I try to run the script, I get the following errors:

    ./graph.sh: line 4: $2: ambiguous redirect

    ./graph.sh: line 8: unexpected EOF while looking for matching `”

    ./graph.sh: line 11: syntax error: unexpected end of file”

    Can you tell me how to fix this?

  3. Are you providing three command line arguments? You have to provide the interval and a file name:

    your_script 5 test.out tag

    Also the end of the sensors line is missing a quote:
    $3; } >>$2
    Should be
    $3; }’ >>$2

    Sorry about that.

Leave a Reply