Skip to content

Instantly share code, notes, and snippets.

@meneguite
Last active February 21, 2021 01:38
Show Gist options
  • Save meneguite/956debf7cde5032a473cbf166fe8fd11 to your computer and use it in GitHub Desktop.
Save meneguite/956debf7cde5032a473cbf166fe8fd11 to your computer and use it in GitHub Desktop.
get-osx-temperature
// Run command:
// sudo powermetrics --samplers smc | node get-osx-temperature-pipe.js
process.stdin.resume();
process.stdin.setEncoding('utf8');
let cpuTemperature = 0;
let gpuTemperature = 0;
let fanRpm = 0;
process.stdin.on('data', function (chunk) {
chunk.split("\n").filter((l) => {
if (l.startsWith('CPU die temperature:')) {
cpuTemperature = Number(l.replace(/[^\d.]/g,''));
}
if (l.startsWith('GPU die temperature:')) {
gpuTemperature = Number(l.replace(/[^\d.]/g,''));
}
if (l.startsWith('Fan:')) {
fanRpm = Number(l.replace(/[^\d.]/g,''));
}
});
if (cpuTemperature) {
console.log('');
console.log(`CPU Temperature: ${cpuTemperature ? cpuTemperature.toFixed(2) : '-'} C`);
console.log(`GPU Temperature: ${gpuTemperature ? gpuTemperature.toFixed(2) : '-'} C`);
console.log(`Fan: ${fanRpm ? fanRpm.toFixed(2) : '-'} rpm`);
console.log('----------------------------------------------------------------------------');
}
});
process.stdin.on('end', function () {
callback(data)
});
// Expected result:
//
// CPU Temperature: 56.27 C
// GPU Temperature: 51.50 C
// Fan: 2180.00 rpm
// ----------------------------------------------------------------------------
// Run command:
// sudo node get-osx-temperature-runner.js
const { spawn } = require('child_process');
const metrics = spawn('powermetrics', ['--samplers', 'smc']);
let cpuTemperature = 0;
let gpuTemperature = 0;
let fanRpm = 0;
metrics.stdout.on('data', (data) => {
data.toString().split("\n").filter((l) => {
if (l.startsWith('CPU die temperature:')) {
cpuTemperature = Number(l.replace(/[^\d.]/g,''));
}
if (l.startsWith('GPU die temperature:')) {
gpuTemperature = Number(l.replace(/[^\d.]/g,''));
}
if (l.startsWith('Fan:')) {
fanRpm = Number(l.replace(/[^\d.]/g,''));
}
});
if (cpuTemperature) {
console.log('');
console.log(`CPU Temperature: ${cpuTemperature ? cpuTemperature.toFixed(2) : '-'} C`);
console.log(`GPU Temperature: ${gpuTemperature ? gpuTemperature.toFixed(2) : '-'} C`);
console.log(`Fan: ${fanRpm ? fanRpm.toFixed(2) : '-'} rpm`);
console.log('----------------------------------------------------------------------------');
}
});
metrics.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
metrics.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
// Expected result:
//
// CPU Temperature: 56.27 C
// GPU Temperature: 51.50 C
// Fan: 2180.00 rpm
// ----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment