Home Server autoStandby.js
If you want to wake up a server in your LAN you use Wake-on-lan, but what do you do if you want to automatically switch it off?
My situation is that I've a server which is also used as desktop to browse some internet pages, so some normal techniques doesn't apply:
- desktop: GNOME/Unity/Ubuntu have built-in suspend after X minutes of inactivity (user level)
- server: You can check if your cpu load is low and then pm-suspend it
Problems
- If I'm downloading a file remotely via ssh + screen the desktop user is inactive so the computer is suspended by desktop suspend.
- If I'm reading a page as desktop user the cpu load is low so the computer is suspended by the server script.
What to do?
Here is my solution: autoStandby.js . You need to disable desktop user level suspend and install 'xprintidle' utility (apt-get has it).
// autoStandby.js var os = require('os');// tick cicle
setInterval(function(){// check low workload
var cpuload = os.loadavg()[2];
console.log(new Date()+ ' cpuload: ' +cpuload);
if (cpuload < 0.22) { // set your idle load, mine is 0.22// check idle user var exec = require('child_process').exec, child; child = exec('sudo -u your-desktop-user env DISPLAY=:0.0 xprintidle', function (error, stdout, stderr) { // do stanby console.log('xstdoutms: ' + stdout); var xidleMs = stdout; if (xidleMs > 60 * 1000) { console.log('Goodbye crul world!!!'); var exec = require('child_process').exec, child; child = exec('pm-suspend', function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } }); } console.log('xstderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } });
}
}, 3 * 60 * 1000); // check every 3mins
Then you can run it with "node autoStanby.js" via screen, upstart or systemd.
Limitations
- It runs as root
- If the computer is used by more than one desktop user, the script must be tweaked
- I know probably a bash version would be simpler, but I think to extend it in future with a web ui
What do you think? You have a better solution? Let me know it!