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

Jan*_*ard 5 javascript process popen node.js

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

Lin*_*iel 4

你可以尝试抓住SIGINT你的孩子:

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

// parent.js
var fork = require("child_process").fork,
    child = fork(__dirname + "/child.js");
Run Code Online (Sandbox Code Playgroud)

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

这是Node.js 邮件列表上关于该主题的一个有启发性的讨论,尽管已经很老了。