Take the 2-minute tour ×

Ok I am faced with a problem in an App that I am currently building using Node.JS. I want the following functionality. I have a script that outputs 'hi', sleeps for a second, outputs 'hi', sleeps for 1 second, and so on and so forth. Now I thought I would be able to tackle this problem with this model.

    var spawn = require('child_process').spawn,



share|improve this question
Can you be more specific about your code? –  Jacob Groundwater Jan 15 '13 at 9:03
I just need to be able to run an separate script and display the output of that script as it is being written to the buffer. currently the script has to finish running for me to see any output. –  foklepoint Jan 15 '13 at 20:48
add comment

2 Answers

up vote 8 down vote accepted

i'm still getting my feet wet with Node.js, but i have a few ideas. first, i believe you need to use execFile instead of spawn; execFile is for when you know have the path to the script, whereas spawn is for executing a well-known command that Node.js can resolve against your system path.

1. provide a callback to process the buffered output:

    'arg1', 'arg2', 'arg3', 
], function(err, stdout, stderr) { 
    // Node.js will invoke this callback when the 

2. add a listener to the child process's stdout stream (9thport.net)

child.stdout.on('data', function(data) {

further, there appear to be options whereby you can detach the spawned process from Node's controlling terminal, which would allow it to run asynchronously. i haven't tested this yet, but there are examples in the API docs that go something like this:

    'arg1', 'arg2', 'arg3', 
], { 
    // detachment and ignored stdin are the key here: 
    detached: true, 
    stdio: [ 'ignore', 1, 2 ], 
child.stdout.on('data', function(data) {
share|improve this answer
add comment


setInterval(function() {


// fork'd children use the parent's stdio
share|improve this answer
add comment

Your Answer


