5

I've got two server scripts (both are relying on socket.io; running on different ports).

I'd like to start both in parallel via gulp. But in addition I'd like to have a possibility to stop one of them. And maybe even access the console output of each script.

Is there any existing solution for this? Or would you even recommend using anything else than gulp?

2 Answers 2

4

I found a solution in which I additionally start a mongoDB server:

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 (e.g. 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']);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm open to suggestions and willing to give the checkmark to better solutions.
0

nodemon/foreverjs is a good solution for not complicated cases, but they are not as scalable as pm2 is. So, if you want a scalable and reliable solution I'd recommend to use pm2. Also, it worth mentioning that pm2 daemonizes after start unlike foreverjs/nodemon. It could be a bug or a feature for you and generally depends on your needs.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.