更新:
您可以注册一个处理程序,
process.on('exit')并在任何其他情况下(SIGINT或未处理的异常)进行调用
process.exit()
process.stdin.resume();//so the program will not close instantlyfunction exitHandler(options, exitCode) { if (options.cleanup) console.log('clean'); if (exitCode || exitCode === 0) console.log(exitCode); if (options.exit) process.exit();}//do something when app is closingprocess.on('exit', exitHandler.bind(null,{cleanup:true}));//catches ctrl+c eventprocess.on('SIGINT', exitHandler.bind(null, {exit:true}));// catches "kill pid" (for example: nodemon restart)process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));//catches uncaught exceptionsprocess.on('uncaughtException', exitHandler.bind(null, {exit:true}));


