i need capture in custom stream outputs of spawned child process.
child_process.spawn(command[, args][, options])
for example,
var s = fs.createwritestream('/tmp/test.txt'); child_process.spawn('ifconfig', [], {stdio: [null, s, null]})
now how read /tmp/test.txt
in real time?
it looks child_process.spawn
not using stream.writable.prototype.write
nor stream.writable.prototype._write
execution.
for example,
s.write = function() { console.log("this never printed"); };
as as,
s.__proto__._write = function() { console.log("this never printed"); };
it looks uses file descriptors under-the-hood write child_process.spawn
file.
doing not work:
var s2 = fs.createreadstream('/tmp/test.txt'); s2.on("data", function() { console.log("this never printed either"); });
so, how can stdout
contents of child process?
what want achieve stream stdout
of child process socket. if provide socket directly child_process.spawn
stdio
parameter closes socket when finishes, want keep open.
update:
the solution use default {stdio: ['pipe', 'pipe', 'pipe']}
options , listen created .stdout
of child process.
var cmd = child_process.spaw('ifconfig'); cmd.stdout.on("data", (data) => { ... });
now, ante, more challenging question:
-- how read stdout
of child process , still preserve colors?
for example, if send stdout
process.stdout
so:
child_process.spawn('ifconfig', [], {stdio: [null, process.stdout, null]});
it keep colors , print colored output console, because .istty
property set true
on process.stdout
.
process.stdout.istty // true
now if use default {stdio: ['pipe', 'pipe', 'pipe']}
, data read stripped of console colors. how colors?
one way creating own custom stream fs.createwritestream
, because child_process.spawn
requires streams have file descriptor.
then setting .istty
of stream true
, preserve colors.
and need capture data child_process.spawn
writes stream, since child_process.spawn
not use .prototype.write
nor .prototype._write
of stream, need capture contents in other hacky way.
that's why child_process.spawn
requires stream have file descriptor because bypasses .prototype.write
call , writes directly file under-the-hood.
any ideas how implement this?
you can without using temporary file:
var process = child_process.spawn(command[, args][, options]); process.stdout.on('data', function (chunk) { console.log(chunk); });
Comments
Post a Comment