根据文档,child_process.spawn
我希望能够在前台运行子进程并允许节点进程本身退出,如下所示:
handoff-exec.js
:
'use strict';
var spawn = require('child_process').spawn;
// this console.log before the spawn seems to cause
// the child to exit immediately, but putting it
// afterwards seems to not affect it.
//console.log('hello');
var child = spawn(
'ping'
, [ '-c', '3', 'google.com' ]
, { detached: true, stdio: 'inherit' }
);
child.unref();
它没有看到ping
命令的输出,而是简单地退出,没有任何消息或错误。
node handoff-exec.js
hello
echo $?
0
那么......是否有可能在 node.js 中(或根本)在父级退出时在前台运行一个子级?
错误节点版本
我发现删除console.log('hello');
允许孩子运行,但是,它仍然没有将前台标准输入控制传递给孩子。这显然不是故意的,因此我当时使用的节点版本肯定有问题......