gpt4 book ai didi

linux - Node.JS 子进程在父进程死亡时被杀死

转载 作者:IT老高 更新时间:2023-10-28 23:09:47 25 4
gpt4 key购买 nike

我正在使用 child_process.spawn() 从我在 Ubuntu 上运行的 Node.JS 应用程序启动一个脚本。据我所知,标准 fork 或派生的 *nix 进程通常不会在父进程死亡时死亡,但是当从 Node.JS 派生进程时,它们似乎在我的应用程序崩溃时被杀死,或者被 ctrl-c 等中止.

为什么会这样,有没有办法解决这个问题?我似乎在 child_process API 中找不到任何明显的选项。

我的应用程序启动了一些应该在后台运行的相当长时间运行的任务,如果我的 Node 服务器崩溃或由于其他原因重新启动,我不想中断任务,而是希望 Node 服务器来备份并优雅地恢复监控这些正在运行的任务的进度。

最佳答案

你需要设置分离选项

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 ]
});

关于linux - Node.JS 子进程在父进程死亡时被杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30708801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com