Last active
February 21, 2021 01:38
-
-
Save meneguite/956debf7cde5032a473cbf166fe8fd11 to your computer and use it in GitHub Desktop.
get-osx-temperature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
// ---------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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