Gulp:并行运行多个节点脚本

我有两个服务器脚本(都依赖于socket.io;运行在不同的端口)。

我想通过喝水并行开始。 但除此之外,我想有可能阻止其中之一。 甚至可能访问每个脚本的控制台输出。

有没有现有的解决scheme呢? 或者,你甚至会build议使用其他的东西而不是吞咽?

我find了一个我另外启动一个mongoDB服务器的解决scheme:

var child_process = require('child_process'); var nodemon = require('gulp-nodemon'); var processes = {server1: null, server2: null, mongo: null}; gulp.task('start:server', function (cb) { // The magic happens here ... processes.server1 = nodemon({ script: "server1.js", ext: "js" }); // ... and here processes.server2 = nodemon({ script: "server2.js", ext: "js" }); cb(); // For parallel execution accept a callback. // For further info see "Async task support" section here: // https://github.com/gulpjs/gulp/blob/master/docs/API.md }); gulp.task('start:mongo', function (cb) { processes.mongo = child_process.exec('mongod', function (err, stdout, stderr) {}); cb(); }); process.on('exit', function () { // In case the gulp process is closed (eg by pressing [CTRL + C]) stop both processes processes.server1.kill(); processes.server2.kill(); processes.mongo.kill(); }); gulp.task('run', ['start:mongo', 'start:server']); gulp.task('default', ['run']); 

对于不复杂的情况, nodemon/foreverjs是一个很好的解决scheme,但它们nodemon/foreverjs那样可伸缩。 所以,如果你想要一个可扩展和可靠的解决scheme,我build议使用pm2 。 另外,值得一提的是pm2在启动后foreverjs/nodemon不像foreverjs/nodemon 。 这可能是一个错误或function ,一般取决于你的需求。

 pm2 start script1.js pm2 start script2.js pm2 status // show status of running processes pm2 logs // tail -f logs from running processes