Joe Flateau
Joe Flateau

Reputation: 1225

PM2 dev and prod environments on same server

I have a dev and prod instance of a microservice running on one machine. My ecosystem file gives them the same name. PM2 will not start the second instance when the first is running due to the name (presumably). Any ideas for a workaround here?

Upvotes: 3

Views: 1405

Answers (3)

holem
holem

Reputation: 125

It's possible to make separated config files for each environment.

dev.config.js:

module.exports = {
  apps: [
    {
      name: "dev-app",
      script: "./app.js",
      env: {
        NODE_ENV: "development"
      }
    }
  ]
};

prod.config.js

module.exports = {
  apps: [
    {
      name: "prod-app",
      script: "./app.js",
      env: {
        NODE_ENV: "production"
      }
    }
  ]
};

And then:

pm2 startOrRestart prod.config.js
pm2 startOrRestart dev.config.js

This approach works with pm2 deploy as well.

Upvotes: 0

Joe Flateau
Joe Flateau

Reputation: 1225

Thanks for the help... I ended up with this:

require("dotenv").config();

module.exports = {
  apps: [
    {
      name: "app-" + process.env.environment,
      script: "./app.js",
      max_memory_restart: "150M"
    }
  ]
};

Upvotes: 1

A1Gard
A1Gard

Reputation: 4168

Pm2 do not sensitive filename because the name is usual for human not for PM2 process.

The usual mistake:

  • Two script try to listen one port.
  • conflict between scripts & resources.

The solution is start first process with pm2 and second process normal start with nodejs and you can see the log and error if not start, or started successfully then start second process first, first process second.

and you can use pm2panel for easily add or remove process in pm2.

Upvotes: 0

Related Questions