Node.js detect a child process exit
Asked Answered
D

1

10

I am working in node, as it happens via a visual studio code extension. I successfully create child processes and can terminate them on command. I am looking to run code when the process unexpectedly exits, this appears to be what the "exit" event is intended for, but I'm unclear on how to call it, this is the code I am working with, the process runs, but does not detect/log on exit, note that output.append is visual studio code specific version of console.log():

        child = exec('mycommand', {cwd: path}, 
        function (error, stdout, stderr) { 
            output.append('stdout: ' + stdout);
            output.append('stderr: ' + stderr);
            if (error !== null) {
                output.append('exec error: ' + error);
            }
        });

        child.stdout.on('data', function(data) {
            output.append(data.toString()); 
        });

Here's four things I have tried that do not work in logging on exit:

        child.process.on('exit', function(code) {
            output.append("Detected Crash");
        });

        child.on('exit', function(code) {
            output.append("Detected Crash");
        });

        child.stdout.on('exit', function () {
            output.append("Detected Crash");
        });

        child.stderr.on('exit', function () {
            output.append("Detected Crash");
        });
Dermoid answered 19/1, 2016 at 16:0 Comment(1)
Which version of node.js are you running?Endocarditis
R
12

Looking at the node.js source code for the child process module, the .exec() method does this itself:

child.addListener('close', exithandler);
child.addListener('error', errorhandler);

And, I think .on() is a shortcut for .addListener(), so you could also do:

child.on('close', exithandler);
child.on('error', errorhandler);
Rodie answered 19/1, 2016 at 17:31 Comment(8)
This is correct, the child.on('close', exithandler) does call when the child process ends. Now I have another issue. My child process spawns more processes, I can force kill child and associated children processes, but what I also want to do is kill the top level child when it's spawned sub processes fail/close. The problem is that I don't execute the children of the childs processes thus have no reference in node, they are initiated automatically by a CLI tool (mycommand in my example is an installed CLI tool). Any ideas how I might resolve this?Dermoid
@Dermoid - Glad we got your first issue fixed. One beauty of working with node.js is you can always just go look at the source code. I find the node.js documentation somewhat lacking, so the source code in pretty handy for answering questions like this. For your additional issue, I'd suggest you create a good description of the new problem and open a new question for that issue since it's pretty much a new issue.Rodie
Okay just created a new question per my comment: #34884555Dermoid
This might have unexpected side effects when the exit handler and the error handler is the same function. In such case, it might get called twice.Dropsy
@JeroenBollen - Where in my answer do I ever suggest that an exit and close handler should be the same function?Rodie
@Rodie Where in my comment did I ever suggest that you suggested they should be the same? You never provided a solution for the case where they are equal, hence I am just leaving a comment for future readers so they know if they have a same handler for exit and error, they should not use this method.Dropsy
@JeroenBollen - OK, it just seems a bit out of the blue as it doesn't seem to have anything to do with either my answer or the OP's question. I thought you were commenting that my answer was missing something.Rodie
@Rodie It does relate to the question very much! The OP wanted to print "Detected Crash" when the script crashed. In your solution, it might print this multiple times per crash!Dropsy

© 2022 - 2024 — McMap. All rights reserved.