Macs have quite a few hardware and temperature sensors, but the only CLI option I’d run across is the tool that comes with Temperature Monitor.app, which is a binary. I was thinking that you’d have to use some sort of frameworks and compiled Obj-C to access these readouts, but while mucking around in the guts of my machine trying to write a system monitoring script, I noticed that the information is available deep within ioreg, apparently undocumented and slightly hidden (all the numbers are multiplied by 65536).
Update: See comments, this may not be that accurate at all. 
So I was able to write a script that displays all the hardware sensor readouts of my machine. It turns out that all the different macs have different sensors and the formatting in ioreg can also vary, but after getting some dumps from friends with different macs, and visiting an Apple Store and looking in the guts of a few of them, I was able to make a script that should return all the hardware sensor values (if any — the mac mini seems to have none) of any recent mac, without having to write a special case for each one.
The formatting of the display may be a little wierd (then again it may not be) but this is so it will work even on dual G5s, which have an absurd amount of obscure sensors, and are not available for me to test on.
Also, the script can be pretty easily scripted itself to make the display prettier, and I am willing to fix up displays for individual machines if you send me what the readout looks like . Please, anyone that tests this leave a comment or email telling me how it works and what machine you are running and improve this thing.
But yeah, maybe I just haven’t seen where this has been done before, but I think it’s pretty sweet to be able to get these readouts through bash.
Here’s the script. Save it, chmod +x, throw it in your PATH, and use “tp -h” for usage instructions.
#!/bin/bash
#
- TP Probe v.0.1
#
- Gets stats from hardware sensors, such as temperatures, voltages,
- and fanspeeds. For Mac OS X. Sensors vary depending on machine,
- Powermacs have lots, down to mac mini’s, which don’t seem to have
- any. Formatting may be rough, because there is wide variation
- between machines, and I wanted to capture all possible sensors on
- as many macs as possible and I only own one.
#
- If you run this and it works, or doesn’t work, or looks awful, or
- anything, please send me an email, preferably including the output
- of tp -l so I can make this better.
#
- ————————————————————————————————-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- http://www.gnu.org/licenses/gpl.html
#
- Copyright © 2005 Rupa Deadwyler djbidi@gmail.com
- test operating sytem
if [ `uname` == “Darwin” ]
then
- help message
if [ “$1” == “-h” ]
then
basename=`echo $0 | sed ‘s/.*\///’`
echo “usage: $basename [option]”
echo “ -l also show Machine Name and CPU Type”
echo “ -h display this help message”
echo “ -e diplay only lines that match regex”
exit
fi
- if -l display Machine Name and CPU Type
if [ “$1” == “-l” ]
then
system_profiler SPHardwareDataType | awk ‘/Machine Name/ || /Machine Model/ || /CPU Type/’ | sed ‘s/[^:]*: //’ | awk ‘{ printf $0 “ “ }’
echo
fi
- sensor data
sensors=`ioreg -n IOHWSensor | awk ‘/location/ || /current-value/ || /“type”/’ | sed -e ‘s/[^”]*”//’ -e ‘s/” =//’ -e ‘s/location//’ -e ‘s/type//’ -e ‘s/”//g’ | awk ‘{ d=($2/65536); if ($1==“current-value”) print substr(d,1,7) “\t” ; if ($1!=“current-value”) print $0 }’ | sed -e ‘s/temperature/Celsius/’ -e ‘s/voltage/volts/’ -e ‘s/fanspeed/fan RPM/’ -e ‘s/current/Amps/’ -e ‘s/^temp$//’ -e ‘s/ //’ | awk ‘{ if ((NR % 3) == 0) print $0; else printf $0 “ “ }’`
if [ “$1” "-e" ]
then
if [ "$2" “” ]
then
echo no regex selected
else
echo “$sensors” | awk “/$2/”
fi
else
echo “$sensors”
fi
- error message if unsupported machine
else
echo -e “\nThis appears not to be an OS X machine\n”
fi
The script is also available here: cutup.org/code/sh
Hello,
I wrote a similar thing as a dashboard widget (http://www.smartsoft.com), and found that the problem is actually quite a bit more complex than expected.
Your function, by coincidence, outputs a value that is ’close’ on some machines when compared to values spewed by some other tools that do the correct interpretation leading to believe that the problem is a simple one.
The correct reading for powerbooks goes like this: The value written in the registry is actually encoded to the hardware sensor. On most machines, the value is a 32 bit int, with the upper word being the degrees in C and the lower word the fractional part of the temperature.
On other machines the value may not even be a temperature, but just some seed for the fan, or needed to be read from the fan itself. In those cases the actual temp cannot be deduced by looking at this value without having the calibration data for the fan (in prom). As you can see this ends up being quite a bit of work it requires custom code for each of the machines or sensor types.
Dammit!
From looking at your site, it appears you are saying that this should be close on some machines, but may be off by up to 7C ?
Or is my script not even that reliable? I could live with 7C
The way that I look at it, this is just fun stuff. But no, the way that you are calculating the temp is incorrect. From my perspective, it is just neat to know if its sunny inside the machine.
If you want to be closer, you can if you implement your calculation as I have outlined. On some machines like the imac g5, you get closer by dividing the value by 10, but its not accurate by any means. For the powerbook g4 you should be dead on.
The open-source utility “X Resource Graph” shows the values of various sensors. Perhaps it would be useful to look at their source code?
http://www.gauchosoft.com/Software/X%20Resource%20Graph/
I’ve been working on some stuff with the cputemp too. Here’s a commandline tool to print out the temperatures. I just stole the code from above mentioned XRG:
temp.dmg.gz
temp.m
Cheers, I sent the results from your command-line tool to get displayed by quicksilver:
set temps to do shell script “/Applications/temp | awk ‘{print $0}’”
tell application “Quicksilver” to show large type temps
return temps
hey, i really like the script! i use Temperature Monitor a lot but it’s often nice to have a quick command line tool for getting temperature readings when you don’t want to fill your Dock with clutter. i was wondering if there’s a command line method of converting celcius to farenheit for those of us who live in strange places that still use that system.