Jump to navigationJump to search

This is a PHP script I created to show the current temperature of the local physical machine it is being from from on a web page.

This is a VERY basic PHP script using very basic commands and variables. At the heart of the script is the "sensors" tool, built into most Linux distributions. It is also known as lm_sensors, and is part of the Red Hat lm_sensors package. The basic command, "sensors" spits out all available hardware sensors readings/current values with temperatures shown in Celsius by default. You can give senors the -f command to change it to displaying temperatures in Fahrenheit values instead, as seen in my simple script, shown here. You can name it anything you want, as long as it's filename extension is .php so that the PHP engine knows how to process the code.

<!DOCTYPE html>
<meta charset="UTF-8" lang="EN">
<meta name="description" content="My Local Machine CPU Temp">
<meta name="keywords" content="Linux sensors temp cpu php script">
<meta name="author" content="Jamie Rubinstein">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Use this meta-tag to force browser to auto-refresh every 10 seconds: -->
<meta http-equiv=refresh content=10>
My Local Machine CPU Temp
<p style="text-align:center; font-size:46px; font-weight:bold">
My Current CPU Temperature on this computer at
<br /><br />
$DegreeSymbol = '\\°';
$Time = exec('date +%r');
echo "$Time";
echo "<br /><br />";
$MyCMD = "sensors -f | grep CPU: | awk '{print $2}' | awk '{print substr($0, 2)}'";
$Temp = exec($MyCMD);
echo "$Temp" . "$DegreeSymbol" . "F";

Save the .php file somewhere within your publicly viewable website directory structure, as long as you remember where you saved it so you can then call it from within a web browser.