15

I'm trying to capture standard output from a spawned child_process in Node.js (0.10.29).

Right now I'm just trying with ping.

The following code doesn't print (but does ping)

var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var util = require('util')

var ping = spawn('ping', ['127.0.0.1'], {stdio: 'pipe'});

ping.stdout.on('data', function(data){
    util.print(data);
})

ping.stderr.on('data', function(data){
    util.print(data);
})

If I change stdio: 'pipe' to stdio: 'inherit' and get rid of the stdout/stderr hooks like so:

var ping = spawn('ping', ['127.0.0.2'], {stdio: 'inherit'});

// ping.stdout.on('data', function(data){
//     util.print(data);
// })

// ping.stderr.on('data', function(data){
//     util.print(data);
// })

I get

PING 127.0.0.2 (127.0.0.2): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1

If I change the address from 127.0.0.2 to 127.0.0.1, which I know will respond to the pings and use the original code I get

PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.060 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.152 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.124 ms

Why are stdout/stderr not firing data events when the ping isn't pinging or inheriting?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dbenny
  • 151
  • 1
  • 3
  • what do you mean by ` doesn't print (but does ping)` ? – Mritunjay Jul 08 '14 at 13:26
  • The `ping` process runs and pings localhost but the node program doesn't print to stdout. – dbenny Jul 08 '14 at 13:51
  • but I copied your code and it's working fine I think. – Mritunjay Jul 08 '14 at 13:54
  • @Mritunjay - which section of code? The trouble code is the first bit of code with 127.0.0.2 – dbenny Jul 08 '14 at 14:12
  • sorry I just copied & pasted first part with 127.0.0.1 – Mritunjay Jul 08 '14 at 14:14
  • but for that also it's working. I'm using `node v0.11.13` unstable. – Mritunjay Jul 08 '14 at 14:16
  • Anyone try with v0.10.25 or 29? – dbenny Jul 08 '14 at 14:18
  • I changed it to `var ping = spawn('ping', ['-v', '-c', '10', '127.0.0.2'], {stdio: 'pipe'});` and after the ping runs through all ten requests and ends it outputs `PING 127.0.0.2 (127.0.0.2): 56 data bytes Request timeout for icmp_seq 0 Request timeout for icmp_seq 1 Request timeout for icmp_seq 2 Request timeout for icmp_seq 3 Request timeout for icmp_seq 4 Request timeout for icmp_seq 5 Request timeout for icmp_seq 6 Request timeout for icmp_seq 7 Request timeout for icmp_seq 8 --- 127.0.0.2 ping statistics --- 10 packets transmitted, 0 packets received, 100.0% packet loss` – dbenny Jul 08 '14 at 14:26
  • Also @Mritunjay I just tried it on 0.11.13 and couldn't get it to print – dbenny Jul 08 '14 at 15:05
  • are you getting warning like `util.print: Use console.log instead`. Try with console.log once. – Mritunjay Jul 09 '14 at 02:41
  • 2
    Even with console.log. Basically I've narrowed it down - Node doesn't seem to get stdout until the process has finished in some cases - others it can get a live update. Not sure what the difference between these two are. – dbenny Jul 09 '14 at 15:01
  • Did you ever find a solution to this? A way to force stdout to be parsed in a stream instead of waiting until the command finishes? – Boushley Mar 18 '16 at 05:26
  • 2
    It actually works fine in latest node version. Probably, you'd just better update yours! – blade091 Apr 05 '16 at 12:15
  • Can you update you node.js version? – fp007 Jul 17 '23 at 15:49

1 Answers1

0

There have been a lot of issue fixes as well as feature improvements with respect to console printing, on the lines of chunking and buffering. As the issue isn't reproducible any more, I would assume that this could be due to one of the underdocumented behaviors of the then Node.js, based on how many bytes of data is available for printing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gireesh Punathil
  • 1,344
  • 8
  • 18