Node.js:进程生成挂起

Node.js: Process Spawn Hang

我正在尝试 运行 以下 shell 命令:

netstat -nat | grep 3000

最终

netstat -nat | grep 3000 | grep ESTABLISHED

从Node.js根据https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options使用spawn获取连接到特定端口的IP地址可见如下:

const netStat = spawn('netstat', ['-nat']);
const grep = spawn('grep', ['3000']);

netStat.stdout.on('data', (data) => {
  grep.stdin.write(data);
});

console.log('Determining public ip\'s connected to port 3000');
grep.stdout.on('data', function(data){
  console.log(data.toString());
});

但是结果就是挂了,是我哪里做错了吗? 在此先感谢您的帮助!

由于您不从 stderr 管道读取并且不使用选项忽略 stdio,因此进程挂起,因为管道在读取之前仍处于打开状态。

一个类似但不相关的问题是 Why is my Node child process that I created via spawn() hanging?