I want to just kill child process, and keep master process.

96 views
Skip to first unread message

Liu Wei

unread,
May 24, 2014, 4:45:50 AM5/24/14
to nod...@googlegroups.com

I use Node.js version 0.10.28

  • I want to just kill child process, and keep master process. But when I run process.exit(0); in child.js. then the master process also has been killed. How should I handle this?

My Code

Master.js

var cp = require('child_process'),

    fork = cp.fork('./server/rpc/download.js');

    fork.send({ zip: '111' });

    fork.on('message', function(msg){

        if(msg.status === 'done') {

            fork.kill('SIGTERM');

        } 

    });

    fork.onUnexpectedExit = function (code, signal) {

      process.exit(1);

    };

    fork.on("exit", fork.onUnexpectedExit);

Child.js

process.on('message', function(m) {

    if(m.zip === '111' ) {

        //Do something logic

        process.send({status: 'done'});

    }

});



process.on('uncaughtException',function(err){

    console.log("fetch.js: " + err.message + "\n" + err.stack + "\n Stopping background timer");

});


// SIGTERM AND SIGINT will trigger the exit event.

process.once("SIGTERM", function () {

    console.log('SIGTERM');

    process.exit(0);

});

    

Ryan Schmidt

unread,
May 24, 2014, 3:13:02 PM5/24/14
to nod...@googlegroups.com

On May 24, 2014, at 3:45 AM, Liu Wei wrote:

> I use Node.js version 0.10.28
>
> • I want to just kill child process, and keep master process. But when I run process.exit(0); in child.js. then the master process also has been killed. How should I handle this?

The master process will exit when there is nothing left for it to do.

What else would you like the master process to do after you kill the child process?


Liu Wei

unread,
May 25, 2014, 2:00:38 AM5/25/14
to nod...@googlegroups.com
Thanks ryandesign. this child_process is just for handling download csv files and then  zip them. So the master process will do display csv files data and CRUD with Nosql.

Ryan Schmidt

unread,
May 25, 2014, 8:09:18 AM5/25/14
to nod...@googlegroups.com
On May 25, 2014, at 1:00 AM, Liu Wei wrote:

> Thanks ryandesign. this child_process is just for handling download csv files and then zip them. So the master process will do display csv files data and CRUD with Nosql.

Is that in your code now? It wasn't in the code you showed us. Again, if there is nothing else for a node app to do, it will quit. If all the master is doing is waiting for a child process to exit, then when the child process exits, the master will also exit.

Additionally, your code requests that the master process exit whenever the child process exits. It says so right here:


fork.onUnexpectedExit = function (code, signal) {
process.exit(1);
};
fork.on("exit", fork.onUnexpectedExit);


You invent a new property on the child object called "onUnexpectedExit" and declare it as a function that exits the master when called. Then you ask node to call that function when the child emits the "exit" event. The "exit" event is emitted when the child exits, regardless of its code, so the property name "onUnexpectedExit" that you've chosen is misleading; the only reference I found to a property of that name is in this blog, so I guess you copied it from there:

https://www.exratione.com/2013/05/die-child-process-die/

If you only want to exit on unusual conditions, you should first check that the code argument is nonzero.


Here is a revised version that shows that the master stays running if you give it something else to do (like print a message once a second):


/* master.js */

var cp = require('child_process'),
fork = cp.fork('./child.js');
fork.send({ zip: '111' });
fork.on('message', function (msg) {
if (msg.status === 'done') {
fork.kill('SIGTERM');
}
});
fork.on('exit', function (code, signal) {
if (code) {
console.log('child exited unexpectedly with code', code);
process.exit(1);
} else {
console.log('child exited normally');
}
});

// Simulate other work happening in the master.
var i = 0;
setInterval(function () {
console.log(++i % 2 ? 'tick' : 'tock');
}, 1000);


/* child.js */

process.on('message', function (m) {
if (m.zip === '111' ) {
//Do something logic
//Simulate task taking 2.5 seconds to complete
setTimeout(function () {
process.send({status: 'done'});
}, 2500);
}
});

process.on('uncaughtException', function (err) {
console.log("child.js: " + err.message + "\n" + err.stack + "\n Stopping background timer");

Liu Wei

unread,
May 25, 2014, 8:29:11 AM5/25/14
to nod...@googlegroups.com
Thank you very much Ryan. It works for me. Also I'm sorry for lack of some code. :)
Reply all
Reply to author
Forward
0 new messages