Home > Ubuntu > How to monitor CPU temperature through terminal prompt?

How to monitor CPU temperature through terminal prompt?

This is an interesting problem when setting up an Ubuntu server. The room that temporary contains the server is not well air-conditioned and as such temperature is a major concern. However, since the machine is headless, it is a bit harder to figure out the CPU temperature without needlessly looking at the sensor all day. Below is instructions on how to setup the CPU monitor on the prompt so it becomes a bit easier to determine whether the computer is overheating.

1. Install/Setup lm-sensors

This is a simple process for in Ubuntu (and probably most distributions). The command to install them is:

# Install lm-sensors to the machine.
>sudo apt-get install lm-sensors

Next, run the command ‘sensors-detect’ and go ahead and say YES to all the options. This is needed in order to detect any hardware sensors on the machine. Reboot afterwards.

# Setup sensors so they register properly.
>sudo sensors-detect

Now, verify that lm-sensors is setup correctly by typing ‘sensors.’ There should be some information about CPU temp/fans/voltage.

# Display sensor information.
>sensors
lm85b-i2c-0-2e
Adapter: SMBus I801 adapter at c400
V1.5:        +1.47 V  (min =  +0.00 V, max =  +3.32 V)
VCore:       +1.36 V  (min =  +0.00 V, max =  +2.99 V)
V3.3:        +3.39 V  (min =  +0.00 V, max =  +4.38 V)
V5:          +5.13 V  (min =  +0.00 V, max =  +6.64 V)
V12:        +12.31 V  (min =  +0.00 V, max = +15.94 V)
CPU_Fan:    2130 RPM  (min =    0 RPM)
fan2:          0 RPM  (min =    0 RPM)
fan3:          0 RPM  (min =    0 RPM)
fan4:       2712 RPM  (min =    0 RPM)
CPU Temp:    +40.0°C  (low  = -127.0°C, high = +127.0°C)
Board Temp:  +29.0°C  (low  = -127.0°C, high = +127.0°C)
Remote Temp: +29.0°C  (low  = -127.0°C, high = +127.0°C)
cpu0_vid:   +1.413 V

2. Write script to continually gather CPU temp.

This is a simple script that gets the CPU temperature out of the sensor data. It assumes that there is only one entry called ‘CPU Temp:’ Some systems may have more than one sensor that displays CPU Temp, so adjust the script accordingly.

cpu_temp.sh

#! /bin/bash
# Records the CPU temp and writes it to a temporary file.
while [ 1 ]; do
     sensors | grep "CPU Temp" | sed 's/.*:\s*+\(.*\)  .*(.*/\1/' >& \
     /tmp/.cpu_temp;
sleep 5;
done

Not particularly fond with this hack script, but it works. Currently, it is set to scan the temperature every 5 seconds, though this can easily be adjusted from the sleep command. Five seconds should be sufficient enough to keep the temperature up-to-date without using too many CPU resources.

3. Tie the CPU temperature to the terminal prompt.

Since Ubuntu uses bash as the default shell, the below changes are for bash prompt. To determine what shell is running, use the follow commands:

# Find which shell is running right now.
>echo $0

For bash, it is simple by modifying the following environment variable PS1. Since Ubuntu has some default setting on terminal prompt, I tack the CPU monitor along with Ubuntu’s defaults. Different shells will have to use similar techniques to set the prompt.

# Default Ubuntu settings
# PS1=${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
PS1='[`cat /tmp/.cpu_temp`] ${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Put this under ~/.bashrc so that everytime a terminal is created, it’ll run this command (for other shells, it’ll be a similar rc file such as cshrc, tschrc, etc).

4. Have the script start during bootup.

One more step to go: the script needs to run in the background for this to work. Just manually starting it through “./cpu_temp.sh &” will not survive if the machine needs a reboot.

First, move the above cpu_temp.sh to /etc/cpu_temp/cpu_temp.sh. It is possible to change it to another place, but make sure to edit the below script. Add the following code to the /etc/init.d/cpu_temp.

/etc/init.d/cpu_temp

#!/bin/sh
#
# Simple script to enable/disable CPU temperature monitor.
PIDFILE="/var/run/cpu_temp.pid"
NAME="cpu_temp"
DAEMON="/etc/cpu_temp/$NAME.sh"

# Return 0, process already started.
# Return 1, start cpu_temp
do_start()
{
        if [ -f $PIDFILE ]; then
                return 0
        fi
        $DAEMON &
        echo "$!" > $PIDFILE
        return 1
}

# Return 0, process not started.
# Return 1, kill process
do_stop()
{
        if [ ! -f $PIDFILE ]; then
                return 0
        fi
        kill -9 `cat $PIDFILE`
        rm $PIDFILE
        return 1
}

case "$1" in
  start)
        do_start
        case "$?" in
                0) echo "$NAME already started." ;;
                1) echo "Started $NAME." ;;
        esac
        ;;
  stop)
        do_stop
        case "$?" in
                0) echo "$NAME has not started." ;;
                1) echo "Killed $NAME." ;;
        esac
        ;;

  status)
        if [ ! -r "$PIDFILE" ]; then
                echo "$NAME is not running."
                exit 3
        fi
        if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
                echo "$NAME is running."
                exit 0
        else
                echo "$NAME is not running but $PIDFILE exists."
                exit 1
        fi
        ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|status}" >&2
        exit 1
        ;;
esac

exit 0

(Note: I tried to make it as generic as possible so all distributions will work. This is why it isn’t using Ubuntu-specific commands).

Last of all, add it to the bootup script. The below command is specific debian-distributions:

# Add cpu_temp to startup sequence.
> sudo update-rc.d /etc/init.d/cpu_temp defaults

That should be it! When the computer reboots, it will automatically run the CPU temperature in prompt.

One final note: make sure that all the scripts are executable.

About these ads
Categories: Ubuntu
  1. September 19, 2009 at 7:17 am

    Hello there,
    Nice post, I just found it and I am already a fan.

  2. Max
    January 16, 2012 at 7:26 pm

    Thanks !
    Thats is exacly what i needed !

  3. Don
    September 12, 2012 at 11:43 am

    Nice post, how to ‘shutdown’ LINUX if temperature too high?

    • coder_commenter
      October 13, 2012 at 8:18 pm

      This shouldn’t be too difficult. Given that you have the core temperature already written to a file, you can do a cronjob that will examine and run ‘shutdown’ command once the number is too high.

  1. March 15, 2011 at 12:45 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: