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

后端 未结 2 2037
礼貌的吻别
礼貌的吻别 2021-01-06 08:37

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 pro

2条回答
  •  逝去的感伤
    2021-01-06 09:05

    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.

提交回复
热议问题