javascript - How to read command line output -
i want open batch file in nodejs , read output, while printing (not @ 1 time when batch finished).
for example, jenkins / hudson.
i'm aware work on windows. finish illustration of how helpful, i'm pretty new nodejs.
i believe you'll fine using node's kid process run command , monitor it's output prints. not have restrictions on windows though, may missing in you're asking. allow me explain in hopes answers question.
imagine have file outputs text, on time , not @ once. let's name file print-something.js
. (i realize you've spoken batch file, know child_process
can execute executable file. here i'll running javascript file via node, execute batch file in same way.)
print-something.js
var maxruns = 5, numruns = 0; function printsomething() { console.log('some output'); settimeout(function() { numruns++; if (numruns < maxruns) { printsomething(); } }, 1000); } printsomething();
it's not of import file does, if study you'll see prints "some output" 5 times, prints each statement 1 sec gap in-between. running command line (via node print-something.js
) result in:
some output output output output output
so have file outputs text in delayed manner. turning our attending file reads output, have this:
monitor-output.js
var spawn = require('child_process').spawn, command = spawn('node', ['print-something.js']); command.stdout.on('data', function(data) { console.log('stdout: ' + data); }); command.on('close', function (code) { console.log('child process exited code ' + code); });
this file spawns process node print-something.js
, , begins inspect standard output. each time gets data, prints console. finally, when execution has completed, outputs exit code. running node monitor-output.js
results in:
stdout: output
stdout: output
stdout: output
stdout: output
stdout: output
child process exited code 0
most won't printing output of file console, that's we're doing here illustration. gives thought on how monitor output of file while runs, , within child_process
.
javascript node.js
No comments:
Post a Comment