【问题标题】:How to prevent child node processes from getting killed with the parent node process?如何防止子节点进程被父节点进程杀死?
【发布时间】:2012-03-07 20:41:45
【问题描述】:

我正在使用 child_process.spawn/child_process.fork 从 node.js 应用程序启动多个子进程。当使用 Ctrl-C 停止父进程时,子进程也会停止。有没有一种优雅的方法可以让子进程保持运行?

【问题讨论】:

    标签: javascript node.js process popen


    【解决方案1】:

    您可以尝试在您的孩子身上捕捉SIGINT

    // child.js
    process.on("SIGINT", function() { console.log("sigint caught") });
    
    // parent.js
    var fork = require("child_process").fork,
        child = fork(__dirname + "/child.js");
    

    运行parent.js 并按^C。您的child.js 应该继续在后台运行。我不知道这在child.js 的生命周期中会产生什么影响。

    这是一个关于 on the node.js mailing list 主题的启发性讨论,尽管已经很老了。

    【讨论】:

      【解决方案2】:

      您可以在父级中调用childprocess.disconnect(); 或在子级中调用process.disconnect();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-19
        • 1970-01-01
        • 2011-09-23
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        相关资源
        最近更新 更多