Auto Shutdown on Overheat
I wrote a program to automatically halt a Linux box when it gets too hot.
Why did I do this ? Well, I have a box running in a very badly ventilated cupboard and it tends to get pretty hot in there. Now when PCs get hot, stuff fails. After losing a hard drive to heat death, I wanted to make sure it did not happen again.
This is a cheap machine made of junk and spare parts which lives under my TV so I can watch the odd DivX movie and muck about with DVB cards, it’s hardly mission-critical so I’m not going to spend loads of cash on a sophisticated cooling system. This script is very simple and works fine.
Prerequisites
To make it work, you need to have lm_sensors installed and configured properly. This can be a little tricky and the procedure varies for different models of motherboard so unfortunately I cannot give you step-by-step instructions. You’ll have to read the manual I’m afraid.
Once that is working, you need to set the limits on each sensor. This is done by editing the file /etc/sensors.conf
Note that whenever you make any changes to /etc/sensors.conf, you must run the following command to commit your changes to the driver:
# sensors -s
If all is good, you should be able to type sensors and see a result like this:
$ sensors as99127f-i2c-0-2d Adapter: SMBus Via Pro adapter at e800 VCore: +1.68 V (min = +1.52 V, max = +1.84 V) +3.3V: +3.57 V (min = +3.14 V, max = +3.79 V) +5V: +5.08 V (min = +4.73 V, max = +5.24 V) +12V: +12.10 V (min = +10.82 V, max = +13.19 V) -12V: -12.46 V (min = -13.22 V, max = -10.74 V) -5V: -6.14 V (min = -6.14 V, max = -3.76 V) fan1: 0 RPM (min = 0 RPM, div = 2) fan2: 5037 RPM (min = 2836 RPM, div = 2) fan3: 0 RPM (min = 0 RPM, div = 2) M/B Temp: +32°C (high = +60°C, hyst = +58°C) CPU Temp: +46.0°C (high = +80°C, hyst = +78°C) (beep) temp3: -31.5°C (high = +122°C, hyst = +121°C) vid: +1.600 V (VRM Version 9.0) alarms: beep_enable: Sound alarm enabled
My little C program will look at any sensor item whose name has “Temp” in it (“CPU Temp” and “M/B Temp” in the above example). If these items exceed the preset limits, the machine will be cleanly shut down.
You can download the source code here:
sensor-alert.c (2 kB)
Installation Instructions
To install it, first compile it:
# gcc -o sensor-alert sensor-alert.c
Then install it in /usr/sbin
# mv sensor-alert /usr/sbin
And finally, add the following line to /etc/crontab
* * * * * root /usr/sbin/sensor-alert
And there you have it. Hopefully this machine won’t be cooking itself now.
And yes, I know you could probably do it in three lines of PERL but I’m a C programmer so shoot me.
