PM2 change cluster processes size at runtime

You can use pm2 scale to scale vertically the number of process at runtime, note that it only work with cluster mode. Example :

  • pm2 scale APPNAME 2 will scale the process to exactly 2 instances.
  • pm2 scale APPNAME +2 will add two process.
  • pm2 scale APPNAME -1 will remove one process.

source link


specify pm2 settings in json format:

{
 "apps": [{
    "name": "server",
    "script" : "index.js",
    "instances": 2,
    "exec_mode: "cluster",
    "cwd": "/path/to/script"
 }]
}

start the server:

pm2 start application.json

suppose you want to add 2 more instances, just run the same command again:

pm2 start application.json

check the processes list:

pm2 list

to test that all 4 instances are run in cluster mode:

pm2 restart server

it will restart each of the 4 processes.