1

Is it possible to get notified when the system is about to change its power state to sleep?

Something like

process.on('sleep', () => foo())

When going to sleep, the process does not get killed or exit, so answers from doing a cleanup action just before node.js exits do not suffer.

Flimzy
  • 68,325
  • 15
  • 126
  • 165
Thomas
  • 2,310
  • 2
  • 22
  • 30

1 Answers1

0

Your programm receive informations from your Operating System in linux from signals. I guess the signal you are looking for is from the following list:

enter image description here


To handle signals in node.js here is how you do it :

// Begin reading from stdin so the process does not exit.
process.stdin.resume();

process.on('SIGINT', () => {
  console.log('Received SIGINT. Press Control-D to exit.');
});

// Using a single function to handle multiple signals
function handle(signal) {
  console.log(`Received ${signal}`);
}

process.on('SIGINT', handle);
process.on('SIGTERM', handle);
Orelsanpls
  • 20,654
  • 4
  • 36
  • 61
  • Thanks. Unfortunately, none of these events get fired when the power state changes. Not even SIGPWR. – Thomas Sep 28 '18 at 13:55