【问题标题】:child process not calling close event子进程未调用关闭事件
【发布时间】:2014-03-19 20:33:02
【问题描述】:

我有以下 node.js 代码:

var testProcess = spawn(item.testCommand, [], {
  cwd: process.cwd(),
  stdio: ['ignore', process.stdout, process.stderr]
});

testProcess.on('close', function(data) {
  console.log('test');
});

waitpid(testProcess.pid);
testProcess.kill();

然而 close 方法永远不会被调用。

我正在寻找的最终结果是我创建了一个进程并且脚本等待该子进程完成(waitpid() 正确执行)。我希望子进程的输出/错误显示在屏幕上(stdio 配置正确执行)。我还想在关闭事件中执行的子进程关闭时执行代码(也尝试退出),但它不会触发。

为什么事件没有触发?

【问题讨论】:

    标签: node.js child-process


    【解决方案1】:

    http://nodejs.org/api/process.html

    Note that just because the name of this function is process.kill, it is really just a signal sender, like the kill system call. The signal sent may do something other than kill the target process.

    您可以在 Kill() 调用时指定信号。

    【讨论】:

      【解决方案2】:

      查看waitpid() 我发现它返回一个带有exitCode 的对象。我更改了代码,以便仅根据 exitCode 的值执行某些操作。

      【讨论】:

        猜你喜欢
        • 2017-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-19
        • 2011-12-30
        • 1970-01-01
        • 2011-10-21
        相关资源
        最近更新 更多