Node.js: child_process.spawn no output standard output unless 'inherit'
Asked Answered
A

1

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?

Aureole answered 8/7, 2014 at 13:17 Comment(14)
what do you mean by ` doesn't print (but does ping)` ?Ordure
The ping process runs and pings localhost but the node program doesn't print to stdout.Aureole
but I copied your code and it's working fine I think.Ordure
@Ordure - which section of code? The trouble code is the first bit of code with 127.0.0.2Aureole
sorry I just copied & pasted first part with 127.0.0.1Ordure
but for that also it's working. I'm using node v0.11.13 unstable.Ordure
Anyone try with v0.10.25 or 29?Aureole
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 lossAureole
Also @Ordure I just tried it on 0.11.13 and couldn't get it to printAureole
are you getting warning like util.print: Use console.log instead. Try with console.log once.Ordure
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.Aureole
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?Infelicity
It actually works fine in latest node version. Probably, you'd just better update yours!Brainless
Can you update you node.js version?Groundhog
V
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.

Volley answered 13/11, 2016 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.