Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node detecting termination

Tags:

node.js

Is it possible for a node script to detect if it was terminate from "external" (p.e.: kill) or internal by a script error?

To be more precise: The first case should't not be a main process that "observe" its children, or a additional script that "looks" if the main script is still running.

2nd case: without putting everything in try-except-blocks. I m looking for a function that is called when an error would stop the script.

like image 962
inselberg Avatar asked Sep 17 '26 14:09

inselberg


1 Answers

var exceptionOccured = false;

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

process.on('exit', function(code) {
    if(exceptionOccured) console.log('Exception occured');
    else console.log('Kill signal received');
});
like image 60
jgillich Avatar answered Sep 19 '26 08:09

jgillich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!