- Posted on
- • Questions and Answers
Read CPU temperature via `/sys/class/thermal/thermal_zone*/temp`
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Understanding and Monitoring CPU Temperature in Linux
Introduction
One of the most critical aspects of managing a Linux system, especially in environments involving physical servers or high-performance computing, is monitoring the CPU temperature. High temperatures can lead to reduced performance, hardware malfunctions, and system crashes. Thankfully, Linux provides a way to track the temperature through the file system. In this article, we'll explore how to read CPU temperature details via the /sys/class/thermal/thermal_zone*/temp
directory.
Q&A: Reading CPU Temperature in Linux
Q1: What is the /sys/class/thermal/thermal_zone*/temp
directory?
A1: In Linux, the /sys/class/thermal/thermal_zone*/temp
directory is part of the sysfs (System Filesystem) that exposes information and interfaces provided by the kernel. It contains files representing each thermal zone detected by the system. Each thermal zone can correspond to one or multiple hardware components that generate heat, including the CPU.
Q2: How can I read the CPU temperature from this directory?
A2: The CPU temperature can be read directly from the command line using the cat command on the appropriate temp
file under the /sys/class/thermal/thermal_zone*/
directory. Each thermal_zone
may correspond to different components depending on your hardware setup and kernel, so you might need to check which zone corresponds to the CPU.
Q3: How can I identify which thermal zone is for the CPU?
A3: This can vary depending on the system. However, you can usually determine this by checking the type
file in each thermal_zone directory, which describes the component the zone corresponds to. For example:
cat /sys/class/thermal/thermal_zone0/type
If the output is something indicative of a CPU or a core, then thermal_zone0
is likely your CPU's thermal zone.
Understanding CPU Temperature Readings
Linux usually reports temperatures in millidegrees Celsius. For example, a reported temperature of 53000
means 53.0
degrees Celsius. To get a readable output, you usually need to divide the result by 1000.
Simple Bash Script to Monitor CPU Temperature
Here’s a basic executable script that continuously monitors the CPU temperature and outputs it in a human-readable format:
#!/bin/bash
# Assuming the CPU temperature is at thermal_zone0
THERMAL_ZONE="/sys/class/thermal/thermal_zone0/temp"
if [ ! -f "$THERMAL_ZONE" ]; then
echo "CPU temperature sensor not found."
exit 1
fi
while true; do
TEMP=$(cat $THERMAL_ZONE)
echo "Current CPU Temperature: $(($TEMP / 1000))°C"
sleep 5 # updates temperature reading every 5 seconds
done
To run this script:
1. Save it to a file, say monitor_temp.sh
.
2. Make it executable with the command chmod +x monitor_temp.sh
.
3. Run it by ./monitor_temp.sh
.
Conclusion
Monitoring the CPU temperature is crucial for maintaining the health and performance of your Linux systems. By utilizing the native /sys/class/thermal
interface, system administrators and users can easily integrate temperature monitoring into their scripts or monitoring tools. This ensures that systems run within safe thermal conditions, enabling preventive actions if temperatures get too high, thus safeguarding your data and hardware from thermal-related issues.
Understanding and leveraging the power of the Linux filesystem to monitor system metrics like temperature not only optimizes performance but also fortifies operational stability and efficiency.
Further Reading
Here are some further reading materials related to monitoring and managing CPU temperature, particularly with a focus on Linux systems:
Linux Thermal Daemon (thermald) Overview: Learn how the Linux Thermal Daemon functions for dynamic thermal management. Linux Thermal Daemon
Advanced CPU Temperature Monitoring Tools: Discover more sophisticated tools available for Linux to help track CPU and other hardware temperatures. Advanced Monitoring Tools
Understanding System Filesystem (sysfs) in Linux: A comprehensive guide on sysfs, which is used to access and interact with kernel objects. Understanding sysfs
Stress Testing and Thermal Testing on Linux: Guides on how to conduct CPU stress tests and monitor the thermal response in a Linux environment. CPU Stress Testing Linux
Scripting for System Monitoring in Linux: A tutorial on writing effective scripts for monitoring various system metrics, including CPU temperature. Linux System Monitoring Scripts