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

后端 未结 2 2033
礼貌的吻别
礼貌的吻别 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.

    0 讨论(0)
  • 2021-01-06 09:21

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

    0 讨论(0)
提交回复
热议问题