简体   繁体   中英

How to prevent child node processes from getting killed with the parent node process?

I'm using child_process.spawn/child_process.fork to start a number of child processes from a node.js application. When stopping the parent process with Ctrl-C the child processes are stopped too. Is there an elegant way to keep the child processes running?

You could try catching SIGINT in your child:

// child.js
process.on("SIGINT", function() { console.log("sigint caught") });

// parent.js
var fork = require("child_process").fork,
    child = fork(__dirname + "/child.js");

Run parent.js and press ^C . Your child.js should continue running in the background. I don't know what the effects of this will be during the lifetime of child.js .

Here is an enlightening, albeit pretty old, discussion of the topic on the node.js mailing list .

You could call childprocess.disconnect(); in the parent or process.disconnect(); in the child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM