【问题标题】:How to spawn a child_process with node.js and save the output to a file如何使用 node.js 生成 child_process 并将输出保存到文件
【发布时间】:2018-03-09 14:57:07
【问题描述】:

我正在尝试从我的Node.js 应用程序中生成一个子进程/bin/bash 并使用我的应用程序stdinstdoutstderr 来实现它,同时我想捕获所有内容输入 (stdin) 并输出 (stdout) 到名为 inAndOut.log 的文件中。

var spawn = require('child_process').spawn;
var shell = spawn('/bin/bash', ['-l'], {stdio: 'inherit'});

【问题讨论】:

标签: node.js bash stream child-process spawn


【解决方案1】:

创建一个可写流,并将标准输出通过管道传递给它:

const { spawn } = require('child_process');
const fs = require('fs');

const writeStream = fs.createWriteStream("output-file");
const shell = spawn('/bin/bash', ['-l'], {});

shell.stdout.pipe(writeStream);

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 2013-09-04
    • 1970-01-01
    • 2020-12-12
    • 2019-03-13
    • 1970-01-01
    • 2013-02-21
    • 2021-06-30
    • 2011-02-23
    相关资源
    最近更新 更多