简体   繁体   English

Node.js拦截另一个进程的输出

[英]Node.js intercept output of another process

I'm trying to initiate a few processes from a running Node.js process. 我正在尝试从正在运行的Node.js进程中启动一些进程。 The problem is, that these have to be initiated from a script, and using exec like this: 问题是,这些必须从脚本启动,并使用exec这样执行:

exec("./scripts/run.sh",(err, stdout, stderr)  => {
if (err) {
  console.error(err);
  return;
}
  this.logs = this.logs.unshift(timestamp() + stdout);
  this.errorlogs = this.logs.unshift(timestamp() + stderr);
});

Does not redirect the output I need. 不重定向我需要的输出。 Fork seems to be only for Node.js processes which these are not. Fork似乎仅适用于非Node.js进程。

The processes work just fine and do function as child processes of the main process. 流程运行良好,并且可以充当主流程的子流程。 I just need to actually get the stdout and stderr outputs. 我只需要实际获取stdout和stderr输出。 Any suggestions? 有什么建议么?

child_process.exec() only runs the callback function once, after the exec'ed process has terminated. 在执行的进程终止后, child_process.exec()仅运行一次回调函数。 If you want incremental data from the stdout and stderr of the process while it is still running, use child_process.spawn() and set the options.stdio attribute to make the process's stdout and stderr available to the parent. 如果要在进程仍在运行时从其stdout和stderr中获取增量数据,请使用child_process.spawn()并设置options.stdio属性,以使父进程可以使用该进程的stdout和stderr。

child_process.spawn() returns a ChildProcess object. child_process.spawn()返回一个ChildProcess对象。 To collect the process's output you can establish data handlers for that object's stdout and stderr streams. 要收集进程的输出,可以为该对象的stdoutstderr流建立数据处理程序。 You should also establish handlers for the error event that fires if the process launch fails and the close event that fires when those streams have closed and the process has terminated. 您还应该为error事件的处理程序建立处理程序,该error事件在进程启动失败时触发,而close事件在这些流关闭且进程终止时触发。

It will look something like this: 它看起来像这样:

  child = spawn("./scripts/run.sh", 
                [],
                {stdio: ['ignore', 'pipe', 'pipe']});

  child.on('error', (err) => }
      console.log('child launch failed: ', err);
  });

  child.on('close', (code) => {
      console.log('child ended: ', code);
  });

  child.stdout.on('data', (outdata) => {
      this.logs = this.logs.unshift(timestamp() + outdata);
  });

  child.stderr.on('data', (errdata) => {
      this.errorlogs = this.logs.unshift(timestamp() + errdata);
  });

The gory details are in the Node.js docs at https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options and https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_class_childprocess 详细信息在https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_optionshttps://nodejs.org/dist/latest-v6的Node.js文档中.x / docs / api / child_process.html#child_process_class_childprocess

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM