Capture node.js crash reason

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I have a script written in node.js, it uses 'net' library and communicates with distant service over tcp. This script is started using 'node script.js >> log.txt' command and everything in that script that is logged using console.log() function gets written to log.txt but sometimes script dies and I cannot find a reason and nothing gets logged in log.txt around the time script crashed.

How can I capture crash reason?

回答1:

It's much easier to capture exceptions by splitting stdout and stderr. Like so:

node script.js 1> log.out 2> err.out 

By default, node logs normal output to stdout, which I believe you are capturing with >>.

As noted in the comments, a segmentation fault is a signal put to stderr by the shell, not necessarily your program. See this unix.stackexchange answer for other options.



回答2:

Couldn't you listen to uncaughtException event. Something along the lines of =>

process.on('uncaughtException', function (err) {   console.log('Caught exception: ' + err); }); 

P.S: after that you are adviced to restart process according to this



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!