0

How to run 2 or more node app with pm2? I tried with different ways by referring (https://stackoverflow.com/a/39316957/11983640) and other links also but its not happening. Any solutions?

syed
  • 606
  • 8
  • 11
  • You want to run multiple node projects with pm2 ? or you want to run same project on multiple cores using pm2 ? – Sarfraaz Jan 21 '20 at 05:59

2 Answers2

1

I suppose you want to run with pm2 different apps . For that case generate an ecosystem file with :

pm2 ecosystem

And then set your scripts to run as you want . An example :

ecosystem.config.js

module.exports = {
  apps : [{
    name: 'MyNodeApp',
    script: 'bin/www',
    args: '',
    instances: 1,
    autorestart: true,
    watch: true,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  },{
    name: 'back-up',
    script: './backup',
    args: '',
    instances: 1,
    autorestart: true,
    watch: true,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }],
};

Run your pm2 with :

pm2 start ecosystem.config.js

With the above eco file i start 2 different apps with pm2 the first is my main app (name:MyNodeApp) and the second is a back up script .

pkoulianos
  • 177
  • 2
  • 5
  • Without config.js is it possible? Because I need to write cofig.js in both the app ryt? – syed Jan 21 '20 at 12:41
0

If you want to host multiple node projects on pm2, you can just do it like this

pm2 start path/to/first/node/project/main_file.js --name "project_1"

pm2 start path/to/another/node/project/main_file.js --name "project_2"

Just make sure both projects are running on different port

Sarfraaz
  • 1,273
  • 4
  • 15