简体   繁体   English

Node.js生成子进程计时

[英]Nodejs spawn child process timing

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. 我试图编写一个nodejs脚本来生成观察程序进程和产生另一个进程来启动我的开发服务器,但是我需要观察程序完成构建,在启动开发服务器之前开始监视。 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. 有没有办法知道哪个子进程什么时候都没有输出任何数据,至少我会知道它已经完成/正在等待输入。

If { stdio: 'inherit' } is not required then you could use something like this: 如果不需要{ stdio: 'inherit' }那么您可以使用如下代码:

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' });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM