I'm trying to capture standard output from a spawn
ed 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
?
ping
process runs and pings localhost but the node program doesn't print to stdout. – Aureolenode v0.11.13
unstable. – Ordurevar ping = spawn('ping', ['-v', '-c', '10', '127.0.0.2'], {stdio: 'pipe'});
and after the ping runs through all ten requests and ends it outputsPING 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
– Aureoleutil.print: Use console.log instead
. Try with console.log once. – Ordure