NodeJS:退出父节点,保留子节点



我正在写一个实用程序。该实用程序的一个命令是运行外部应用程序。

var child_process = require('child_process');
var fs = require('fs');
var out = fs.openSync('.../../log/out.log', 'a');
var err = fs.openSync('.../../log/err.log', 'a');
exports.Unref = function(app, argv) {
  var child = child_process.spawn(app, argv, {
    detached: true,
    stdio: [ 'ignore', out, err ]
  });
  child.unref();
  //process.exit(0);
};
目前:

$ utility run app --some-args // run external app
    // cant enter next command while app is running

我的问题是,如果我运行这个命令,终端被锁定,而"外部"应用程序正在运行。

但是终端窗口不应该被child_process锁住。

i wanna run:

$ utility run app --some-args
$ next-command
$ next-command

外部应用程序(桌面应用程序)将自行关闭。

:

$ subl server.js // this runs Sublime Text and passes a file to the editor
$ npm start // The terminal does not locked - i can execute next command while Sublime is still running

你知道我的意思^^吗?

['>>../../log/out.log', '2>>../../log/err.log']附加到argv的末尾,而不是留下两个打开的文件,因为它是打开的文件句柄,使进程保持活跃。

stdio中打开的文件描述符传递到detached: true中不会像您期望的那样工作,因为没有办法在父进程中unref()文件描述符并使其仍然为子进程工作。即使有办法,我相信当父进程退出时,操作系统也会清理(关闭)它打开的文件描述符,这会给分离的子进程带来问题。

唯一可能的方法是将文件描述符传递给子进程,但是这个功能在几个稳定的分支之前被删除了,因为相同的功能在一些其他平台(例如:Windows)上不存在。

最新更新