【发布时间】:2012-03-07 20:41:45
【问题描述】:
我正在使用 child_process.spawn/child_process.fork 从 node.js 应用程序启动多个子进程。当使用 Ctrl-C 停止父进程时,子进程也会停止。有没有一种优雅的方法可以让子进程保持运行?
【问题讨论】:
标签: javascript node.js process popen
我正在使用 child_process.spawn/child_process.fork 从 node.js 应用程序启动多个子进程。当使用 Ctrl-C 停止父进程时,子进程也会停止。有没有一种优雅的方法可以让子进程保持运行?
【问题讨论】:
标签: javascript node.js process popen
您可以尝试在您的孩子身上捕捉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 主题的启发性讨论,尽管已经很老了。
【讨论】:
您可以在父级中调用childprocess.disconnect(); 或在子级中调用process.disconnect();。
【讨论】: