I want some work on process exiting.
But process.on('exit')
is not working using express or socket.io or mysql, etc.
process.on('exit', function() {
console.log('Server exit.');
});
only this code is working.
var mysql = require('mysql');
var conn = mysql.createConnection();
conn.connect();
process.on('exit', function() {
console.log('Server exit.');
});
this is not working.
You can make it work by doing two simple things:
exit
event, use the SIGINT
event (instead of process.on('exit',function(){/*stuff here*/});
use process.on('SIGINT',function(){/*stuff here*/});
forever start your-script.js
, type forever start your-script.js --killSignal=SIGINT
this works for me, but my script doesn't use much mysql and doesn't use any express, so it might not work for you
That's because once you initialize express()
, say (specifically: start the server), or create a MySQL connection, or do anything else that "lives in the background", node will not exit, so will not called you .on('exit')
.
It will not exit because it has pending tasks. It's just like if you were to invoke setInterval()
.
If you disconnect the MySQL connection, or stop the express server, it will then exit and invoke you handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With