Linux – Run Specific Commands with Lm-Sensors When Temperature Exceeds Limits

acpilinuxsensorstemperatureUbuntu

I have a pretty badly ventilated computer whose temperature reaches 100º C in some occasions. The poor thing can't be ventilated any better ("put a bigger fan" is not a suitable solution). When the CPU reaches 100º C, the machine stops "violently" (just shuts down). That machine is running Ubuntu 10.10 with lm-sensors-3 (the installed package is lm-sensors 1:3.1.2-6)

I know what program is causing the issue (a very demanding media player) I could actually stop it for a while without causing major disruptions when the temperature reaches 98º C and boot it again when it reaches… lets say 90º C.

Is that possible to do something like that directly through lm-sensors or do I have to create my own process that checks lm-sensors periodically and "does its things" depending on the temperature?

Thank you in advance.

Best Answer

It depends on what is the output of sensors. If yours is something like mine:

% sensors
k10temp-pci-00c3
Adapter: PCI adapter
temp1:        +44.0°C  (high = +70.0°C)

then you can use the following script, adapting it accordingly. Besides TEMP_STOP and TEMP_START, you should change the regular expression that filters the line from sensors you want to use. It's the parameter to grep, in the temp function.

#!/bin/bash

TEMP_STOP=98
TEMP_START=90

temp() {
    sensors | grep '^temp1:' | sed -e 's/.*: \+\([+-][0-9.]\+\)°C.*$/0\1/'
}

while true; do
    TEMP=$(temp)
    # keep waiting until temp is too hot
    while [ $(echo "$TEMP < $TEMP_STOP" | bc) = 1 ]; do
        sleep 10
        TEMP=$(temp)
    done

    echo temp $TEMP too hot, stopping.

    # now wait for it to cool down...
    while [ $(echo "$TEMP > $TEMP_START" | bc) = 1 ]; do
        sleep 10
        TEMP=$(temp)
    done

    echo ok now, restarting...
done
Related Question