如何防止子节点进程被父节点进程杀死? [英] How to prevent child node processes from getting killed with the parent node process?

查看:116
本文介绍了如何防止子节点进程被父节点进程杀死?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 processes are stopped too. Is there an elegant way to keep the child processes running?

推荐答案

您可以尝试捕获 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 的生命周期中这会产生什么影响。

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.

这篇关于如何防止子节点进程被父节点进程杀死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆