16

I'm running sensors-detect on my system with lm-sensors because my system keep shutting down when I watch youtube videos

So I'm wondering how can I find the temperatures of the fan and cpu manually without a 3rd party software like lm-sensors? I'm just not sure where they're stored.

processor   : 0
vendor_id   : AuthenticAMD
cpu family  : 16
model       : 6
model name  : AMD Athlon(tm) II X2 240e Processor
power management: ts ttp tm stc 100mhzsteps hwpstate

x86_64

wjandrea
  • 14,109
  • 4
  • 48
  • 98
  • What version of Ubuntu are you running? – wjandrea Jul 24 '18 at 21:50
  • 2
    It depends on your processor. For many recent Intel processors the turbostat program will read the MSRs (Machine Specific Registers) and display the package and core temperatures. Edit your question and add your processor make an model. If it is one that I know, I'll answer with how to do it manually. – Doug Smythies Jul 24 '18 at 21:50
  • would the additional RAM i added have a problem.., RAM is 1333 megatransfers per second, and cpu is max MHz: 2800.0000 –  Jul 24 '18 at 22:14
  • I do not know the specific MSRs for that processor. Suggest to try turbostat, just to know if it knows about it. If you want, I can write an answer with how I do it manually for Intel processors. – Doug Smythies Jul 24 '18 at 22:15
  • that was 1333 MT/s for each 4096 RAM, and there's 2, CPU is max set at 2800.000, i just find it odd, that its only when videos play, i wanted to make sure the sensors was detecting it correctly and wasn't pre registered in the code to show a certain temp between two ranges at random –  Jul 24 '18 at 22:33
  • 13
    What do you mean by third-party? How much of Ubuntu do you think is first-party software? – muru Jul 25 '18 at 00:13
  • 1
    I'm not exactly sure how many parties Ubuntu uses before the user intercepts the distribution for use. I know in a general aspect that Ubuntu is a Linux operating system that I referred to as the first party, followed by me and then the question at hand, leaving my questionable program the 3rd party rather than saying an off the wall 6th party variant of or after ubtunu sponsors and or volentary programmer softwares, so I don't exactly know. –  Jul 28 '18 at 10:35

5 Answers5

17

It sounds like what you want is /sys/class/hwmon and /sys/class/thermal.

Both of these provide simple shell based access to the data you want (the hwmon directory will also include other sensor types). Each has one directory for each sensor interface in your system (which may have more than one sensor).

Three other things to note though:

  • Unless you have a really bad system, running the sensors command even once a second shouldn't have any percievable effect. In fact, regularly reading the files in the above mentioned directories may have more of an effect.
  • Graphs are your friend here. I would suggest getting something like netdata or collectd running so you have realtime (or near realtime) data to look at.
  • Check your voltages too. What your describing could also be due to having a power supply that can't keep up with your system's power requirements. This will be indicated by voltages dropping significantly when the system is under high load.
  • This should be the accepted answer, because it deals with the actual question asked: "how can I find the temperatures [...] manually [...]? I'm just not sure where they're stored." Then some `/proc/cpuinfo` showing an AMD processor. Not sure why OP then accepted an Intel-specific answer which requires another tool to be installed. – Aaron F Jul 25 '18 at 12:04
  • 1
    @AaronF : I don't disagree. I wrote an Intel specific answer because the OP asked me to. No other tool needs to be installed? The reason I read package temperatures directly via MSRs rather than via `/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input` is that most of my work is with extremely idle systems, and the latter method has a lot more overhead, resulting in a consistent 4 to 5 degrees higher result. Of course, there is no difference for a heavily loaded system. – Doug Smythies Jul 25 '18 at 12:46
  • 1
    @DougSmythies I thought your answer was very good and complete, which is why I didn't downvote nor leave a negative comment beneath it :-) The rdmsr program is part of msr-tools, which isn't installed by default on my Intel machine (running Ubuntu, maybe other distros install by default if they detect Intel?) – Aaron F Jul 25 '18 at 15:50
  • @AaronF : I forgot that msr-tools is a needed additional package. So my method also needs and additional tool, which is not what the OP wanted. – Doug Smythies Jul 25 '18 at 15:52
  • @DougSmythies nonsense! OP accepted your answer - have an upvote from me as well, for reading between the lines, skillfully avoiding the AMD cpuinfo red herring, and figuring out what OP /really/ wanted! ;-D – Aaron F Jul 25 '18 at 15:57
  • 2
    Hello @ DougSmythies and Aaron F, I had originally chose DougSmythies as the primary answer because he was the first to provide assistance and his answer was very clear on how to search for the Temperature manually as well as using the manual solution into an easy to use program with mdmsr support, It was in fact a hard decesion as both answers were very informative, But since you both agreed that this answer was the best. I reverted the primary answer to Aaron, thank you for your support. Happy hunting –  Jul 28 '18 at 10:46
9

This answer is about one way to monitor processor temperature manually for some Intel processors via accessing the Machine Specific Registers (MSR) directly.

The first thing to note is, in this case, what is read out of the MSR is relative to the Tcc, the limit temperature, so an additional calculation is required to determine the actual temperature.

Refer to the Intel® 64 and IA-32 Architectures Software Developer’s Manual, or in your case the AMD equivalent.

In my case, I want bits 22-16 of the MSR at 0x1B1, aka IA32_PACKAGE_THERM_STATUS. The Tcc for my older i7-2600K is 98 degrees.

Here is a simple script to monitor the temperature (and CPU frequency) manually:

#! /bin/dash
#
# temp_mon3 Smythies 2016.10.05
#       a simplified version of temp_mon2,
#       for monitoring temp.
#       Note: it is on purpose that -a is not used.
#       Also CPU0 frequency (1 is good enough, when all
#       are loaded).
#
# temp_mon2 Smythies 2016.09.29
#       Monitor Package temperatures.
#       Use clock modulation to control temps.
#       i.e. simulate the second to last level
#       of defense.
#       Use simple primatives.
#       run as sudo
#       hardcoded for my tcc of 98 degrees.
#
echo ... begin package temperature monitoring ...

#
# In case I forgot (which I often do)

modprobe msr

#
# first let the drastic effect of the sudo command decay
# Done later in temp_mon3.

#
# some stuff

COMMAND="rdmsr --bitfield 22:16 -u 0x1B1"
COMMAND2="cat /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"

#
# then get on with it

while [ 1 ];do
  sleep 4
  TEMP_RAW=$(eval $COMMAND)
  CPU0_FREQ=$(eval $COMMAND2)
  TEMP_ACT=$((98-TEMP_RAW))
  echo "$TEMP_ACT   $CPU0_FREQ"
done

And here is some sample output, where I add some CPU load after awhile (temp goes from 31 to 73 degrees):

$ sudo ./temp_mon3
[sudo] password for doug:
... begin package temperature monitoring ...
31   1605275
31   1605154
32   1605164
30   1605148
31   1605176
51   3511279
54   3511278
55   3511279
57   3511283
58   3511279
60   3511278
61   3511284
63   3511279
64   3511280
64   3511280
66   3511280
67   3511278
68   3511280
68   3511281
69   3511278
71   3511280
70   3511281
71   3511281
71   3511280
72   3511276
72   3511277
73   3511283
73   3511279
^C
Doug Smythies
  • 14,898
  • 5
  • 40
  • 57
  • 1
    Why do you use `eval`? – wjandrea Jul 24 '18 at 22:48
  • @Doug: Great answer to use msr! However, isn't the right msr register **0x19c** ? And the temperature is TEMP= TCC - [0x19c bits 22:16] . So it is necessary to read the TCC first. – abu_bua Jul 25 '18 at 00:38
  • @abu_bua : I don't know if 0x19c works or not, but what I am using does. And yes, I hardcoded TCC for my processor rather than the better solution which would be to read it from the processor. – Doug Smythies Jul 25 '18 at 01:44
  • @wjandrea : actually, I don't recall why I thought I had to use eval. I do recall having troubles to get it working. Notice that the shell is dash. – Doug Smythies Jul 25 '18 at 01:46
  • @abu_bua: 0x19C does work. I didn't look into it, but I assume one is core temp and one is package temp. – Doug Smythies Jul 25 '18 at 01:51
  • I saw it to late: What you are measuring is the **Package** Temperature, 0x19c is the **Die** Temperature. – abu_bua Jul 25 '18 at 01:52
  • Use functions to store code, not variables: `get_temp_raw() { rdmsr --bitfield 22:16 -u 0x1B1; }` and then `TEMP_RAW=$(get_temp_raw)`. Same for the frequency. – ilkkachu Jul 25 '18 at 10:25
  • rdmsr doesn't work for me, i have wrmsr but it doesn't seem to recognize any command, I've even tried modprobe msr and its there, just not working –  Jul 25 '18 at 15:35
  • 1
    did you installed `sudo apt install msr-tools`? – abu_bua Jul 25 '18 at 15:39
  • msr-tools is already the newest version (1.3-2+b1), then i tried, man msr-tools and got No manual entry for msr-tools and also tried msr-tools --help and ended up with bash: msr-tools: command not found.. msr --version....bash: msr: command not found –  Jul 25 '18 at 21:46
  • 1
    msr-tools is the package. It would be `man rdmsr` and `man wrmsr` for two of the individual commands within the package. – Doug Smythies Jul 25 '18 at 21:52
8

The lm-sensors project (and therefore the sensors command) makes use of the libsensor library; the libraries and package are:

  • Ubuntu 18.04: libsensors4, version 3.4.0-4

     sudo apt install libsensors4 
    
  • Ubuntu 20.04: libsensors5, version 3.6.0.

     sudo apt install libsensors5
    

I also recommend to install the developer package (includes man pages), which is the same for LTS 18.04 and LTS 20.04:

sudo apt install libsensors4-dev

Some background information can no be found on:

man libsensors
man sensors.conf

BTW this is the same library which reads the temperature as seen in the sysdirectory.

To sum it up, sensors is a good choice. Watching the temperature from command-line can be easily done by

watch -n 1 sensors

If you intend to write a program, take a look at the libsensors manual man libsensorsor make use of your /usr/share/doc/ documentation. In a C-program you have to include #include <sensors/sensors.h> headers. It will use the sensors.conffiles /etc/sensors3.conf and/or /etc/sensors.conf. Further (users) configuration can be found in /etc/sensors.d/, if you make use of this option.

If you think that you are missing some sensors take a look at the /sys/class/thermal or the linked /sys/devices/virtual/thermal directory.

To get the temperatures of the all thermal zones use the command

$ cat /sys/devices/virtual/thermal/thermal_zone?/temp

77000
66000
67000

The temperature is measured in miliCelcius (mC), in the case above, the temperature measured in celsius are: 77.0, 66.0, 67.0 °C.

To watch the temperatures continuously use

watch -n 1 cat /sys/devices/virtual/thermal/thermal_zone?/temp

In this directory you can also find information about your cooling (fan) devices, and how the PID regulators are programmed.

Further some over-temperature protection is coded firmware/hardware based (this is a good idea), and its set-up data is placed in your bios.

If you prefer to display the temperature in °C instead of °mC use the bash computation $((value / 1000 )) or awk to make this conversion:

cat /sys/devices/virtual/thermal/thermal_zone?/temp | awk '{printf " %5.2f °C\n" , $1/1000}'
abu_bua
  • 10,473
  • 10
  • 45
  • 62
  • i know there's programs, but i was looking for a way to manually find th etemps –  Jul 24 '18 at 21:59
  • 1
    What do you mean by manually? All that things that prints the temperature are programs. – abu_bua Jul 25 '18 at 18:46
  • i don't have a thermal_zone, all i have are cooling_device0 1 and 2 but they dont hold a file temp, only cur_state device max_state power subsystem type uevent –  Jul 25 '18 at 21:49
  • cur and max state are 0 and 10 –  Jul 25 '18 at 21:50
  • 1
    What exactly are the `thermal_zones`? My 4-core Intel CPU seems just to have a single `thermal_zone` (`sensors`, however, correctly lists all 4 core temperatures). – Gwyneth Llewelyn Aug 01 '19 at 10:02
6

Install Psensor (sudo apt install psensor sensors-applet) from the default Ubuntu repositories and configure Psensor to automatically play an alert sound whenever the temperature goes above a user determined maximum. The purpose of this is to let you hear a warning sound before your system shuts down without adding additional application overhead to your system.

Psensor displays itself on the desktop as a little thermometer icon in the notification area in the upper right corner of the desktop. You can right-click the thermometer icon at any time to display the hardware temperatures.

karel
  • 110,292
  • 102
  • 269
  • 299
0

Since the temperature appears as file (/sys/class/thermal/thermal_zone0/temp) in Linux system, we could find out the answer by reading a file. To find the temperature purely by shell script:

  1. Run the command: touch get_temperature.sh to make a new empty file;

  2. Type the following code below:

#!/bin/sh

TEMP_RAW=$(cat /sys/class/thermal/thermal_zone0/temp)
# TEMP_CEL=$(expr $TEMP_RAW / 1000)
TEMP_CEL=$(echo "scale=2;$TEMP_RAW / 1000" | bc)
TEMP_CEL_W_SIGN=temp=${TEMP_CEL}\'C

# print
echo $TEMP_CEL_W_SIGN
  1. To make the script executable, run: sudo chmod +x get_temperature.sh;

  2. Execute the script by running: ./get_temperature.sh.

Edit: Thanks for the advice in the comment section!

  • Can you please specify where to write these commands? do we need to make a text file with some extension like .sh and run it using some command? Try to give complete answer. – Inmate4587 Oct 15 '20 at 05:06