get system cpu temperature in php

To get the system CPU temperature in PHP on a Linux server, you can use the following code snippet:

main.php
<?php
$temp = exec("cat /sys/class/thermal/thermal_zone0/temp");
$temp = $temp / 1000; // Convert to Celsius
echo "CPU Temperature: " . $temp . "°C";
?>
153 chars
6 lines

This code reads the system CPU temperature from the thermal_zone0 file in the /sys/class/thermal directory, which is where the Linux kernel exposes thermal sensor data.

The temperature value is then divided by 1000 to convert it from millidegrees Celsius to degrees Celsius, and the result is echoed to the output with a message.

This method should work on most Linux systems with a thermal sensor, but the exact path to the thermal_zone0 file may vary depending on the system configuration.

gistlibby LogSnag