Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: Process Spawn Hang

I am trying to run the following shell command:

netstat -nat | grep 3000

and eventually

netstat -nat | grep 3000 | grep ESTABLISHED

from Node.js to obtain ip address connected to specific port using spawn according to https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options visible below:

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());
});

But the result just hangs, am I doing something wrong? Thanks in advance for the help!

like image 225
Sterling Butters Avatar asked Oct 16 '25 22:10

Sterling Butters


1 Answers

Since you don't read from the stderr pipe and don't ignore stdio using the options, the process hangs because the pipe is still open until read from.

A similar, but unrelated issue is Why is my Node child process that I created via spawn() hanging? .

like image 92
Cody G Avatar answered Oct 19 '25 13:10

Cody G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!