Skip to content

Instantly share code, notes, and snippets.

@jbutko
Last active June 1, 2017 13:49
Show Gist options
  • Save jbutko/8fe1aaf038f134ace7a479bc28a0c7f2 to your computer and use it in GitHub Desktop.
Save jbutko/8fe1aaf038f134ace7a479bc28a0c7f2 to your computer and use it in GitHub Desktop.
PM2 cluster mode - fix for ports

When you run an app in cluster mode with PM2 each worker will receive a value in process.env.NODE_APP_INSTANCE which goes from 0 to number of workers-1. Let's say you start your app with pm2 start app.js -i 4, then your workers will receive 0 1 2 3 as NODE_APP_INSTANCE value.

Then in your code you can use that value to set the listen port such as server.listen(8000 + process.env.NODE_APP_INSTANCE)

// Unitech/pm2#1510 (comment)

// pm2 user permission fix Unitech/pm2#837

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment