How can node.js process trigger a callback function when it exit? -
that code, tried 2 ways trigger callback function when process exit.
my purpose when process exit, can write word txt file.
the way exit process click 'close' button on node.exe window,the other way key ctrl+c.
but both of these methods not effective.
process.on('exit', function() { chatcache.each(function(i,self,length){ util.writejsontotxt(chatchache_filepath,json.stringify(self)); }); }); //-------------------------------------------- var stdin = process.openstdin(); process.on('sigint', function () { console.log('got sigint. press control-d exit.'); chatcache.each(function(i,self,length){ util.writejsontotxt(chatchache_filepath,json.stringify(self)); }); });
you have kill process sigint handler :
process.on('sigint', function() { console.log('got sigint. going exit.'); //your code execute before process kill. process.kill(process.pid); }); then exit when press ctrl+c .
process.on('exit', function() {}); executed when node.js code completes executing (finishes event loop) without errors, never happen if run server.
Comments
Post a Comment