18

I was wondering if it would be possible to obtain the CPU temperature and embed it into the command prompt.

This is my output for sensors:

$}-sensors
coretemp-isa-0000
Adapter: ISA adapter
Physical id 0:  +55.0°C  (high = +87.0°C, crit = +105.0°C)
Core 0:         +55.0°C  (high = +87.0°C, crit = +105.0°C)
Core 1:         +52.0°C  (high = +87.0°C, crit = +105.0°C)

Could you please show me how to use the grep function to embed the temperature into my command prompt?

  • Possible duplicate of How do I get the CPU temperature? – mondjunge May 31 '16 at 8:56
  • qucik: cat /sys/class/thermal/thermal_zone0/temp , for me it shows 50000, when my temperature is 50°C – mondjunge May 31 '16 at 8:58
  • @mondjunge those files are very hardware dependent. Both the names and the paths will change depending on what hardware you have. On my system, that file shows the temperature of the acpitz-virtual-0 virtual device and that is very different from the temperature of my CPU. – terdon May 31 '16 at 9:07
  • You can use the same code I have in my answer. It will also work for your sensors output. – terdon May 31 '16 at 9:50
  • @terdon: I know but don't have the time to explain, I just mark duplicated questions in my free 5 Minutes and try to hint out a quick solution. – mondjunge May 31 '16 at 11:00
36

Yes, it is possible, but the details depend on your system. In most cases, the command sensors should show it.

  1. Install the necessary package

    sudo apt-get install lm-sensors
    
  2. Run sensors-detect and follow the prompts

    sudo sensors-detect
    
  3. Install any extra drivers if sensors-detect tells you to.

  4. Run sensors to make sure it works

    $ sensors
    acpitz-virtual-0
    Adapter: Virtual device
    temp1:        +27.8°C  (crit = +110.0°C)
    temp2:        +29.8°C  (crit = +110.0°C)
    
    coretemp-isa-0000
    Adapter: ISA adapter
    Physical id 0:  +63.0°C  (high = +105.0°C, crit = +105.0°C)
    Core 0:         +62.0°C  (high = +105.0°C, crit = +105.0°C)
    Core 1:         +63.0°C  (high = +105.0°C, crit = +105.0°C)
    
    nct6776-isa-0a00
    Adapter: ISA adapter
    Vcore:                  +1.86 V  (min =  +0.00 V, max =  +1.74 V)  ALARM
    in1:                    +1.36 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    AVCC:                   +3.33 V  (min =  +2.98 V, max =  +3.63 V)
    +3.3V:                  +3.33 V  (min =  +2.98 V, max =  +3.63 V)
    in4:                    +1.01 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    in5:                    +0.00 V  (min =  +0.00 V, max =  +0.00 V)
    in6:                    +0.21 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    3VSB:                   +3.31 V  (min =  +2.98 V, max =  +3.63 V)
    Vbat:                   +3.18 V  (min =  +2.70 V, max =  +3.63 V)
    fan1:                     0 RPM  (min =    0 RPM)
    fan2:                  3292 RPM  (min =    0 RPM)
    SYSTIN:                  +0.0°C  (high =  +0.0°C, hyst =  +0.0°C)  sensor = thermistor
    CPUTIN:                 +51.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
    AUXTIN:                  +0.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
    PCH_CHIP_CPU_MAX_TEMP:  +58.0°C  (high = +80.0°C, hyst = +75.0°C)
    PECI Agent 0:           +60.0°C  (high = +80.0°C, hyst = +75.0°C)
                                     (crit = +105.0°C)
    PCH_CHIP_TEMP:           +0.0°C  
    PCH_CPU_TEMP:            +0.0°C  
    intrusion0:            OK
    intrusion1:            OK
    beep_enable:           disabled
    
  5. Parse the output to get only the CPU temperature.

    As you can see above, the output on my system is different than yours. However, the line we care about here is the same. You can get the CPU temperature with:

    $ sensors | grep -oP 'Physical.*?\+\K[0-9.]+'
    63.0
    
  6. Edit your ~/.bashrc (or equivalent file if you're using another shell) and add a function that runs the command above:

    show_temp(){
        sensors | grep -oP 'Physical.*?\+\K[0-9.]+'
    }
    
  7. Use the function in your prompt. For example:

    PS1="\u@\h $(show_temp) $ "
    
  • Is really necessary to install lm-sensors in Ubuntu? Is installed by default in Debian. – Braiam May 31 '16 at 17:21
  • 1
    @Braiam It is not installed by default in 15.10 or 16.04. – cat May 31 '16 at 17:26
1

Install lm-sensors:

sudo apt-get install lm-sensors

Detect what sensors are available:

sudo sensors-detect

To show the temperature:

sensors
  • 5
    As I understand the question, the OP wants the CPU temperature to appear in their Bash prompt, like username@hostname (cputemp): /current/directory $ - They already know how to run the sensors command in general, how would they have been able to show the output in the question otherwise? – Byte Commander May 31 '16 at 10:34
  • 3
    @ByteCommander in Bram's defense, the OP didn't mention sensors in their original question and only added the output after I asked for it in the first revision of my answer. Granted, this answer doesn't explain how to show CPU temp only nor how to include it in the prompt, but mentioning sensors was useful information when the answer was posted. – terdon May 31 '16 at 10:55
  • On an AMD system I had to turn on "IOMMU" in the BIOS before I could get the CPU temperature (MSI motherboard with FX8300: Overclocking settings -> CPU Features -> IOMMU, changed "Disabled" to "64 MB" (the only other option)). – Peter Mortensen Jul 16 '16 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.