Nodejs spawn child process timing

389 views Asked by At

I'm trying to write a nodejs script to both spawn a watcher process and spawn another process to start my dev server but I need the watcher to finish building, start watching before the dev server is started. I have tried using timeouts, but difficult to gauge when build is complete.

var spawn = require('child_process').spawn
fileWatcher = spawn('cmd', [args'], { stdio: 'inherit' })
setTimeout(function() {
 devServer = spawn('node', ['server/index.js'], { stdio: 'inherit' })
},20000)

Is there a way of knowing when either child process isn't outputting any data, at least then I'll know it's done/waiting for input.

1

There are 1 answers

0
Punit Mittal On

If { stdio: 'inherit' } is not required then you could use something like this:

var spawn = require('child_process').spawn;
args = ['-f', 'test.log'];
fileWatcher = spawn('tail', ['-f', 'abc.log']);
fileWatcher.stdout.once('data', function(data){
  console.log("Spawning dev server");
  devServer = spawn('node', ['server.js'], { stdio: 'inherit' });
});