1

Is it possible to start and continue to capture output from a certain bash process with node? For example: say I was run tail /some/file, how can I keep listening to every new line printed and act on the output?

2
  • Well, you'd need to attach to the standard-output from the process...not sure if you can do that from node.js. Commented Jul 13, 2012 at 7:33
  • @Bobby Yes you can, very easily as a matter of fact. See the child_process module. :) Commented Jul 13, 2012 at 16:12

2 Answers 2

9
var spawn = require('child_process').spawn,
    tail  = spawn('tail', ['-f', '/tmp/somefile']);
tail.stdout.pipe(process.stdout);

child_process module is well documented

Sign up to request clarification or add additional context in comments.

2 Comments

You can also run exec and it'll gather the entire output of stdout and stderr for you. For an instance like tail that'll work out fine. (If you have a continously running script, monitoring the output streams is a better option)
Yep my script is a continous one, for example tail or Google App Engine's dev_appserver.py. I'll test this and get back to you.
6

For completeness, I've added this answer as well.

You can use child_process.spawn to spawn a process and monitor it's output. However, for a command like tail, cat, etc that don't run long or continuously you can just use child_process.exec and it will capture the entire output for stdout and stderr and give it to you all at once.

var cp = require("child_process");

cp.exec("tail /some/file", function (err, stdout, stderr) {
    // If an error occurred, err will contain that error object
    // The output for the command itself is held in stdout and stderr vars
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.