Detect when parent process exits

12,811

Solution 1

Could you just put an exit listener in the parent process that signals the children?

Edit:
You can also use node-ffi (Node Foreign Function Interface) to call ...
prctl(PR_SET_PDEATHSIG, SIGHUP);
... in Linux. ( man 2 prctl )

Solution 2

a simpler solution could be by registering for 'disconnect' in the child process

process.on('disconnect', function() {
  console.log('parent exited')
  process.exit();
});

Solution 3

This answer is just for providing an example of the node-ffi solution that entropo has proposed (above) (as mentioned it will work on linux):

this is the parent process, it is spawning the child and then exit after 5 seconds:

var spawn = require('child_process').spawn;
var node = spawn('node', [__dirname + '/child.js']);
setTimeout(function(){process.exit(0)}, 5000);

this is the child process (located in child.js)

var FFI = require('node-ffi');
var current = new FFI.Library(null, {"prctl": ["int32", ["int32", "uint32"]]})

//1: PR_SET_PDEATHSIG, 15: SIGTERM
var returned = current.prctl(1,15);

process.on('SIGTERM',function(){
        //do something interesting
        process.exit(1);
});

doNotExit = function (){
        return true;
};
setInterval(doNotExit, 500);

without the current.prctl(1,15) the child will run forever even if the parent is dying. Here it will be signaled with a SIGTERM which will be handled gracefully.

Solution 4

I start Node.JS from within a native OSX application as a background worker. To make node.js exit when the parent process which consumes node.js stdout dies/exits, I do the following:

// Watch parent exit when it dies

process.stdout.resume();
process.stdout.on('end', function() {
  process.exit();
});

Easy like that, but I'm not exactly sure if it's what you've been asking for ;-)

Share:
12,811

Related videos on Youtube

700 Software
Author by

700 Software

Join 700.social ! A happy medium between Facebook and Gab. :) The name is too long but the domain looks good. Also, Software Development / Consulting (423) 802-8971 700software.com old username: George Bailey (but now I use my real name) http://www.google.com/images?q=George+Bailey

Updated on June 04, 2022

Comments

  • 700 Software
    700 Software almost 2 years

    I will have a parent process that is used to handle webserver restarts. It will signal the child to stop listening for new requests, the child will signal the parent that it has stopped listening, then the parent will signal the new child that it can start listening. In this way, we can accomplish less than 100ms down time for a restart of that level (I have a zero-downtime grandchild restart also, but that is not always enough of a restart).

    The service manager will kill the parent when it is time for shutdown. How can the child detect that the parent has ended?

    The signals are sent using stdin and stdout of the child process. Perhaps I can detect the end of an stdin stream? I am hoping to avoid a polling interval. Also, I would like this to be a really quick detection if possible.

  • 700 Software
    700 Software about 13 years
    I think this will work. The parent is killed when it is time to shut down, but I am pretty sure that the exit listener will still work. Perhaps I can still check for a parent process id every few seconds and if it becomes the number 1, that means the parent has died.
  • entropo
    entropo about 13 years
    If you're shutting down, your processes will receive SIGTERM first. You can capture this and do cleanup in this way too: process.on('SIGTERM', function () { console.log('Got SIGTERM, exiting...'); // do some cleanup here... process.exit(0); });
  • entropo
    entropo about 13 years
    And yes, having children periodically check if their PPID became 1 is a good fallback for cases where the parent didn't exit cleanly. See also: stackoverflow.com/questions/269494/…
  • Abdullah Jibaly
    Abdullah Jibaly almost 10 years
    Thank you! I tried everything and this seems to be the only way to catch that your parent process was killed -9.
  • itaifrenkel
    itaifrenkel almost 10 years
    For those implementing this solution. Do not call process.exit(1) until you are actually done. logger.warn("SIGTERM. Exiting gracefully.", function() {process.exit(1);})
  • JakubKnejzlik
    JakubKnejzlik over 9 years
    I totally agree that this is great solution. In my case I'm listening on SIGTERM event from parent process and also (if anything bad happens and I need to restart the parent process) on disconnect event. In my case the problem is noticeable, because child process creates server holding port, so starting again cause "ERRADDRINUSE". This solves the problem :)
  • sak
    sak over 6 years
    I was struggling with this use case and this works brilliantly. Cheers!
  • mpr
    mpr over 5 years
    It looks like node-ffi has been deprecated in favor of newer module "ffi".
  • Nathan Goings
    Nathan Goings over 2 years
    process.stdout.resume() crashes for me.
  • Nathan Goings
    Nathan Goings over 2 years
    Seems like that is an old usage. Remove the resume() started working. Also, turns out the event I was looking for was error

Related