I have a command that I need to execute three times. It generates lots of output to stdout and takes about a minute. After the three commands finish, a fourth (different) command should be executed. A shell script would look like this:
#!/bin/sh
command arg1 arg2
command arg1 arg2
command arg1 arg2
something_else
I was thinking about using child_process.exec()
in a loop but that buffers the stdout until it is finished (right?). I need to give the user immediate feedback so buffering the stdout for more than a second is not a good idea. So I need to use child_process.spawn()
but the asynchronous handling drives me a bit nuts. I was thinking about putting the spawn call in a function like this:
fucnction myfunc(runs) {
if (runs >= 1) {
myproc = spawn(...)
myproc.on('exit',function() { myfunc(runs - 1) })
}
}
which would work I assume. But is there a better way? I am on unix and perhaps windows systems.
the node module async has functionality where you could do all of your jobs in parallel in one piece of code.
Check out async (https://github.com/caolan/async)
also check out cluster (http://nodejs.org/docs/latest/api/cluster.html)