IMD SolutionComputing LibraryShop main store

How-to

Read CPU temperature on a Linux edge PC with lm-sensors

Operating systems on the plant floor Published 5 min read

Short answer

Install lm-sensors, run sensors-detect accepting the Intel thermal sensor, then read the value with sensors or watch sensors. With nothing installed, cat /sys/class/thermal/thermal_zone*/temp and divide each millidegree value by 1000 for degrees Celsius. Zone numbers identify board temperature inputs, not proven CPU cores, so confirm which zone tracks processor load before you label it CPU temperature.

On this page
  1. What the kernel exposes in /sys/class/thermal
  2. Install lm-sensors and run sensors-detect
  3. Read one value, or watch it continuously
  4. Read the thermal zones with no package installed
  5. Worked example: 48146 and 47246 millidegrees from a two-zone edge PC
  6. Use the reading in a soft PLC runtime or a script
  7. Questions asked

Two command-line paths get CPU temperature off a Linux edge PC, and you need only one of them. Install lm-sensors, let sensors-detect find the Intel thermal sensor, and sensors prints a formatted reading while watch sensors refreshes it live. Or install nothing: cat the files under /sys/class/thermal and divide each raw millidegree value by 1000 for degrees Celsius. Both paths read the same kernel thermal zones, and both carry the one caveat that matters on a plant floor: a zone number identifies a board temperature sensor, and only a reading that climbs under processor load is proven CPU temperature.

What the kernel exposes in /sys/class/thermal #

The Linux kernel's thermal sysfs interface registers every thermal zone device as thermal_zone followed by a number, all under /sys/class/thermal. Each zone's temp attribute reports millidegrees Celsius, thousandths of a degree, which is why a file you expect to read 48 holds 48146. On an industrial computer these zones are temperature sensors placed on the board: an edge computer typically exposes two, a compact controller may expose one or none, and the numbering reflects registration order, not a core map.

The same interface defines an offset attribute that platform drivers use in temperature calculations. If the vendor documents an offset for a zone, apply it to the raw value before treating the converted number as an absolute temperature; if none is documented, read temp as-is.

Install lm-sensors and run sensors-detect #

This path gives you named chips and formatted output. Open an SSH session with root privileges and work through the sequence:

  1. Update the package index: apt-get update.
  2. Install the package: apt-get install lm-sensors.
  3. Start detection: sensors-detect.
  4. Answer YES at the prompt to add the Intel thermal sensor.
  5. Answer NO to the remaining scan questions as they come up.
  6. Answer YES at the final prompt, which writes the detected module into /etc/modules.
apt-get update
apt-get install lm-sensors
sensors-detect

The scanner probes a long list of bus types on its way through, and accepting every suggestion loads modules your controller does not need; the NO answers in step 5 prevent that. The final YES matters because /etc/modules is what loads the sensor module at boot, so skipping it leaves sensors with no chip to query after the next restart.

Read one value, or watch it continuously #

sensors prints one formatted reading per detected chip. watch sensors reruns it and refreshes the screen, which is what you want for the verification test: put the processor under load, watch the number climb, drop the load, watch it fall back. That correlation, temperature moving with processor work, is the proof that the sensor you are reading measures the processor and not the case. It is the same load-and-watch check you lean on when troubleshooting high CPU usage in a Node-RED agent running on the same box.

Read the thermal zones with no package installed #

If the image has no package repository, or you do not want to change a qualified controller, the zone files alone are enough. This path needs nothing but shell access and a kernel that exposes the zones:

cat /sys/class/thermal/thermal_zone0/temp
cat /sys/class/thermal/thermal_zone1/temp

Each file returns one raw millidegree value. For scripting, this line reads every zone and returns the values space-separated on a single line, ready to split:

grep -h . /sys/class/thermal/thermal_zone*/temp | xargs | tr -d '\n'

On a two-zone edge computer that line comes back as two numbers such as 48146 47246. Check the directory before you script against fixed zone numbers: if /sys/class/thermal holds no thermal_zone entries, that kernel exposes no zones on that hardware, and the vendor's documented temperature interface is the remaining path.

Worked example: 48146 and 47246 millidegrees from a two-zone edge PC #

Inputs, each with its unit and source:

  • Raw reading from /sys/class/thermal/thermal_zone0/temp: 48146 millidegrees Celsius, at idle, from the one-line read above on a two-zone industrial edge computer.
  • Raw reading from /sys/class/thermal/thermal_zone1/temp: 47246 millidegrees Celsius, same command, same moment.
  • Unit basis: the kernel thermal sysfs interface defines temp in millidegrees Celsius.

Rule in words: degrees Celsius is the raw millidegree value divided by 1000, and the spread between zones is one converted reading minus the other.

Arithmetic, written out:

  • Zone 0: 48146 / 1000 = 48.146 °C
  • Zone 1: 47246 / 1000 = 47.246 °C
  • Spread: 48.146 °C - 47.246 °C = 0.900 °C

Result: the zones read 48.146 °C and 47.246 °C, 0.9 °C apart at idle.

The decision: adopt zone 0's 48.146 °C as the CPU temperature point once one test passes. Run the processor under load, confirm that number climbs and then falls back toward 48.146 °C when the load stops. A zone that stays flat through that test measures the board, and gets a board label, not a CPU label. The 0.9 °C spread is this unit's steady-state reference: both sensors sit on the same thermal mass, so a spread that widens to several times 0.9 °C at unchanged load means one zone has stopped tracking, and you resolve that before trusting either number.

Use the reading in a soft PLC runtime or a script #

Put the one-line read into a small shell script, place it where your runtime looks for call-out scripts, and give it execute permission for its owner (mode 750). The runtime calls the script by name and gets back that single line; split it at the space, convert each field to a real number, divide each by 1000, and you have two temperature tags in engineering units. The mechanics of shelling out differ by runtime, but the script-out, string-back, convert pattern is the one described in running scripts from a soft PLC runtime on Linux.

Keep the CPU label off those tags until the load test says which zone earned it, and keep the reading in its role: a Linux temperature tag is monitoring data, not a protection function. Trips that guard equipment belong in the safety controller or hardwired limits, not in a soft PLC conversion.

Questions asked

Why does a thermal zone file return a number like 48146?

The temp attribute reports millidegrees Celsius, so the file holds 1000 times the temperature you expect. Divide by 1000 and 48146 becomes 48.146 degrees Celsius. Any consumer of the raw value, whether a script, a soft PLC conversion or a log parser, must do that division before the number means anything, and a reading that already looks like a plausible temperature without dividing is off by three orders of magnitude.

Does thermal_zone0 always mean CPU core 0?

No. The kernel registers each thermal zone device as thermal_zone with a running number, and on an industrial computer those zones are temperature sensors placed on the board. The numbering is registration order, not a core map. Confirm what each zone measures by loading the processor and watching which readings move before you label anything as CPU temperature in logic or alarms.

Is reading the thermal zone files safe on a running controller?

Yes. Reading a sysfs file with cat is passive: it changes no configuration, loads no module and writes nothing, which is why it suits a qualified image you are not allowed to modify. The lm-sensors path, by contrast, installs a package and writes to /etc/modules, so treat it like any other software update under your change control.

Should I log one zone or both?

Log both. The zone that tracks load becomes your CPU temperature tag, and the second stays valuable as a board reference: two sensors on the same thermal mass should agree closely at steady state, and a logged pair that diverges while load is unchanged tells you one zone has stopped tracking before either number is wrong. A single tag cannot show you that.

What if /sys/class/thermal has no thermal_zone entries?

Then that kernel image on that hardware exposes no thermal zones, and the no-package path does not exist on the device. This varies by controller family: some units expose two zones, some one, some none. Use the temperature interface the vendor documents for that unit rather than inferring a processor temperature from an unrelated sensor, and record which machines support which method.

Keep reading

  • How-to

    Install Node-RED Natively on a Linux Edge Computer

    Install Node-RED directly on the Debian-based edge computer rather than in Docker: give the login account sudo, install build-essential, git and curl, then run Node-RED's official Debian install script and start the runtime without the -v flag. The script installs Node.js, npm and Node-RED and registers a systemd service, so the runtime also starts at boot.

    Edge computing and IIoT

  • How-to

    Run Python scripts from a soft PLC runtime on Linux

    Call the script by giving the runtime's process-execution function the full string python3 /path/to/script.py, not a shebang. Put the process-execution permission in CODESYSControl_Usr.cfg, the user configuration file that survives runtime updates since release 4.13.0.0, because the main config file is overwritten. Prove the script in the Linux shell first, and containerize Python when the controller has no package feed.

    Edge computing and IIoT

  • Selection guide

    Choosing an operating system image for an industrial PC

    Order the industrial PC with the vendor image closest to your runtime: Windows IoT Enterprise LTSC where the vendor documents it for your model, the vendor's qualified Linux for a Linux stack, or the BSD image if your code builds on FreeBSD. Bring-your-own images are the fallback for monitoring-class loads. Qualify your binaries on the exact model and image; support and real-time behavior are model-specific.

    Operating systems on the plant floor