Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start application with pm2 if starts form gulp

Tags:

node.js

gulp

pm2

Hi I was following a tutorial to build an nodejs app. This tutorial is using gulp, to run my app I just do gulp dev to start all things. In my gulpfile I have:

var fs = require('fs')
var gulp = require('gulp')

fs.readdirSync(__dirname+'/gulp').forEach(function(task){
    require('./gulp/' + task)
})

gulp.task('dev', ['watch:css', 'watch:js', 'dev:server'])

The code in ./gulp/server.js

var gulp = require('gulp')
var nodemon = require('gulp-nodemon')

gulp.task('dev:server', function(){
    nodemon({
        script: 'server.js',
        ext:'js',
        ignore: ['ng*', 'gulp*', 'assets*'],
        env: { 'NODE_ENV': require('../config').ENV }
    })
})

Is it possible somehow to use pm2 with this gulp setup, I saw some questions in stackoverflow and google, but cant get anything done. If someone can help me would be great. Thank you in advance.

like image 616
Alex Avatar asked Sep 27 '15 12:09

Alex


People also ask

Will pm2 start your applications after system reboot?

When the server restarts, it will automatically restart PM2, which will then restart all the Node. js applications/processes it is managing. In this article, we will show you how to deploy PM2 as a service to reliably manage your Node.

Can pm2 run an NPM start script?

Yes. Use pm2 start npm --no-automation --name {app name} -- run {script name} . It works.


1 Answers

It's available at http://pm2.keymetrics.io/docs/usage/pm2-api/

var gulp = require('gulp');
var pm2 = require('pm2');

gulp.task('dev:server', function () {
    pm2.connect(true, function () {
        pm2.start({
            name: 'server',
            script: 'server.js',
            env: {
                "NODE_ENV": require('../config').ENV
            }
        }, function () {
            console.log('pm2 started');
            pm2.streamLogs('all', 0);
        });
    });
});

Implementation from https://github.com/Unitech/PM2/issues/1525#issuecomment-134236549

like image 180
code-jaff Avatar answered Sep 22 '22 17:09

code-jaff