Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can node.js detect when it is closed?

Tags:

node.js

I want to know if it is possible to detect when a node.js process is closed through clicking the 'X' on Windows. If so then how. I have tried process.on("exit"); but that did not work.

like image 373
Aurange Avatar asked Oct 01 '22 19:10

Aurange


1 Answers

Catch SIGHUP

/*
 * Run this code and close the Window before the 10 seconds
 */
var x = setTimeout(function() { console.log('hello world') }, 10000);

process.on('SIGHUP', function() {
  console.log('About to exit');
  process.exit();
});
like image 110
Daniel Avatar answered Oct 05 '22 10:10

Daniel