首页 > 解决方案 > NodeJS子进程在同一个shell中执行2个命令

问题描述

我想在同一个 shell 中使用 nodeJS 运行 2 个命令。但是,文档似乎只能运行一个命令child_process.spawn(command[, args][, options])

必须在同一个 shell 中执行 2 个命令的原因是因为第一个脚本将写入环境,而第二个脚本将从环境中读取。

这是脚本的一个示例,但我希望 ps 和 grep 都在同一个 shell 中。

const { spawn } = require('child_process');
const ps = spawn('ps', ['ax'], { shell: true });
const grep = spawn('grep', ['ssh'],{  shell: true });

标签: node.jschild-process

解决方案


尝试

ps.stdout.pipe(grep.stdin);

推荐阅读