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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Thomas
  • 2,338
  • 2
  • 23
  • 32

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
  • 22,456
  • 6
  • 42
  • 69
  • Thanks. Unfortunately, none of these events get fired when the power state changes. Not even SIGPWR. – Thomas Sep 28 '18 at 13:55