且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止子节点进程被父节点进程杀死?

更新时间:2023-02-05 20:43:24

您可以尝试捕获 SIGINT 您孩子的code>:

// 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 的生命周期中这会产生什么影响。

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.

这是在node.js邮件列表上讨论主题的一个启发性的,虽然已经很久了/ a>。

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