11

ドキュメントによると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');、それでもフォアグラウンド stdin コントロールが子に渡されません。それは明らかに意図したものではないため、当時使用していたノードのバージョンには何かバグがあるに違いありません...

https://github.com/nodejs/node/issues/5549

4

2 に答える 2