2017-07-15 30 views

回答

0

收听uncaughtException事件并记录您收到的任何错误。这会让你了解发生了什么。然后根据需要执行任何清理,并根据需要重新启动应用程序。这可以让你的应用程序从崩溃中“恢复”,如果它打算长时间运行。

//handle crashes and kill events 
process.on('uncaughtException', function(err) { 
    //log the message and stack trace 
    fs.writeFileSync('crash.log', err + "\n" + err.stack); 

    //do any cleanup like shutting down servers, etc 

    //relaunch the app (if you want) 
    app.relaunch({args: []}); 
    app.exit(0); 
}); 

您还可以收听到SIGTERM事件,看看你的应用程序被杀死了,也正常关闭服务器,重启等

process.on('SIGTERM', function() { 
    fs.writeFileSync('shutdown.log', "Received SIGTERM signal"); 

    //do any cleanup like shutting down servers, etc 

    //relaunch the app (if you want) 
    app.relaunch({args: []}); 
    app.exit(0); 
}); 
相关问题