Capture node.js crash reason

I have a script written in node.js, it uses the "net" library and communicates with the remote service via tcp. This script is launched using the node script.js -> log.txt 'command, and everything in this script, which is registered using the console.log () function, is written to log.txt, but sometimes the script dies, and I cannot find the reason , and nothing is logged in log.txt during the script.

How can I capture the cause of the failure?

+6
source share
2 answers

It is much easier to catch exceptions by separating stdout and stderr . For instance:

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

By default, node writes the normal output to stdout, which I assume you capture with >> .

As noted in the comments, a segmentation error is a signal that is placed in the stderr shell, not necessarily your program. See This unix.stackexchange answer for other options.

+4
source

Could you listen to the uncaughtException event . Something along the lines =>

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

PS: after that, you are advised to restart the process in accordance with this article from Felix Heisendorf

+10
source

Source: https://habr.com/ru/post/913022/


All Articles