Node.JS: Detect when parent process exits?

Up vote 2 down vote favorite share g+ share fb share tw.

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.

Node. Js processes watchdog link|improve this question asked Apr 4 '11 at 16:15George Bailey5,0061465 89% accept rate.

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

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.

– George Bailey Apr 6 '11 at 12:03 1 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 Apr 6 '11 at 17:07 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/… – entropo Apr 6 '11 at 17:25.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions