父级死亡时Node.JS子进程被杀死 [英] Node.JS child processes being killed when parent dies

查看:233
本文介绍了父级死亡时Node.JS子进程被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用child_process.spawn()从在Ubuntu上运行的Node.JS应用程序启动脚本.据我所知,标准的分叉或生成的* nix进程通常不会在父进程死后消失,但是当从Node.JS生成进程时,它们似乎在我的应用程序崩溃或被ctrl-c等终止时被杀死.

I am using child_process.spawn() to start a script from my Node.JS application running on Ubuntu. As far as I know, standard forked or spawned *nix processes don't generally die when the parent dies, but when spawning processes from Node.JS, they seem to get killed when my application crashes, or is aborted with ctrl-c etc.

为什么会这样,并且有办法解决吗?我似乎在child_process API中找不到任何明显的选项.

Why is this and is there a way around this? I can't seem to find any obvious option in the child_process API.

我的应用程序启动了一些应该在后台运行的长时间运行的任务,并且如果我的节点服务器崩溃或由于其他原因而重新启动,我不想中断任务,而是希望节点服务器来备份并正常恢复监视那些正在运行的任务的进度.

My application starts some quite long-running tasks that should run in the background, and if my node server crashes or is restarted for some other reason I don't want to interrupt the tasks, instead I want the node server to come back up and gracefully resume monitoring the progress of those running tasks.

推荐答案

您需要设置分离选项

如果设置了分离选项,则子进程将被设置为 新流程小组的负责人.这使孩子有可能 在父级退出后继续运行.

If the detached option is set, the child process will be made the leader of a new process group. This makes it possible for the child to continue running after the parent exits.

var child = spawn('prg', [], {
   detached: true,
   stdio: [ 'ignore', out, err ]
 });

这篇关于父级死亡时Node.JS子进程被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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