Is there any way to get system idle time on all platforms

Viewed 1116

I am making an electron app to check system idle time. I want to use a common module to get idle time on winodws, mac and linux.

I am using this command to get idle time on mac but it is not working in linux. i also tried different npm plugins to get idle time but none of them is working as expected

function idleTime(callback) {
  var command = `ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'`;
  child_process.exec(command, function (err, stdout, stderr) {
    return err ? callback(err) : callback(null, stdout.trim());
  });

my expected result is to check if the user is using system or not. i wanted to calculate idle time of sysem on all platforms (mac,linux,windows).

2 Answers

Too late but for newcomers look at Electron Power monitor API's

const {powerMonitor} = require('electron'); 
const idle = powerMonitor.getSystemIdleTime() // it returns in seconds when I am writing this
console.log('Current System Idle Time - ', idle); 
Related