【发布时间】:2016-09-22 11:32:24
【问题描述】:
我对此感到非常困惑,并且几个月来一直在努力寻找解决方案。我在 OSX 上。
我听说使用 child_process.spawn 并将 detached 选项设置为 true,将启动子进程作为新进程组的领导者,如果父进程退出,子进程可能会继续运行。但是,我从未见过任何证据。
https://nodejs.org/api/child_process.html
例如:
const n = cp.spawn('node', ['watch-transpile.js'], {
detached: true,
stdio: ['ignore']
});
上面是由父级执行的,如果我们运行$ ps aux | grep node
我们得到:
Olegzandr 2546 0.0 0.2 3048544 19564 ?? Ss 11:29PM 0:00.09 node lib/transpile/watch-transpile.js
Olegzandr 2541 0.0 0.7 3115684 60216 s000 S+ 11:29PM 0:01.47 node index -t -a -w
但是当我用 control-c 杀死父进程时,子进程和父进程一样死亡。
如何才能真正创建一个独立于带有节点的父进程的后台进程?这可要了我的命!
【问题讨论】: