Pm2: Change pm2 restart to restart one by one on multiple instances

Created on 14 Apr 2015  路  9Comments  路  Source: Unitech/pm2

Now that feature #1143 is done, will it be ready to change the pm2 restart all (And pm2 restart app) command to restart the instances of the same app one at a time instead of all of them together?

For example, if i have a 6 instance app, instead of doing this:

delete instance 1
delete instance 2
...
delete instance 6

start app 1
start app 2
...
start app 6

it will do it like this:

delete instance 1
start instance 1
delete instance 2
start instance 2
...
delete instance 6
start instance 6

this will help in having at most 1 instance down when restarting, instead of all of them (Making the server basically unavailable for a brief moment).

Most helpful comment

any way to set time to wait until next restart?

4 processes running.

  • restart 1
  • wait 30 seconds
  • restart 2
  • wait 30 seconds
  • restart 3
  • wait 30 seconds
  • restart 4

and if restart 1 fails - stop and leave other 3 running.

while I have a warm-up period of 30 seconds before it starts to listen for connections.

All 9 comments

pm2 reload <app name> does this already ;)

@jshkurti the docs say it works only with cluster_mode, while i'm using fork_mode.

Will it work just the same?

How did you manage to run the same app 6 times in fork_mode and why ?

I don't want to use node's built-in cluster mode, as i rather have each instance listen on different ip and do the load balancing myself using nginx.
I'm using pm2 programmatically to start the different instances, with something like this:

pm2.connect(function(err) {

    // delete all previously started instances
    pm2.delete('all', function(err) {

        // start each instance with an extra environment variable NODE_APP_INSTANCE set to its instance id
        for (var i = 0; i < instances; i++) {

            // start the standard pm2 json file (All configurations are listed there)
            pm2.startJson(path.resolve(__dirname, 'pm2.json'), {
                additional_env: {
                    NODE_APP_INSTANCE: i
                }
            }, function(err) {

                // make sure we only disconnect after all instances were started
                if (++started === instances) {
                    pm2.disconnect();
                }
            });
        }
    });
});

pm2 restart already restarts apps one by one :)
Logs when I do a pm2 restart all while I have 4 apps running :

2015-04-14 16:37:41: Stopping app:child id:0
2015-04-14 16:37:41: App name:child id:0 disconnected
2015-04-14 16:37:41: App name:child id:0 exited with code SIGTERM
2015-04-14 16:37:41: Process with pid 12103 killed
2015-04-14 16:37:41: Starting execution sequence in -cluster mode- for app name:child id:0
2015-04-14 16:37:41: App name:child id:0 online
2015-04-14 16:37:41: Stopping app:child id:1
2015-04-14 16:37:41: App name:child id:1 disconnected
2015-04-14 16:37:41: App name:child id:1 exited with code SIGTERM
2015-04-14 16:37:41: Process with pid 12112 killed
2015-04-14 16:37:41: Starting execution sequence in -cluster mode- for app name:child id:1
2015-04-14 16:37:41: App name:child id:1 online
2015-04-14 16:37:41: Stopping app:child id:2
2015-04-14 16:37:41: App name:child id:2 disconnected
2015-04-14 16:37:41: App name:child id:2 exited with code SIGTERM
2015-04-14 16:37:41: Process with pid 12133 killed
2015-04-14 16:37:41: Starting execution sequence in -cluster mode- for app name:child id:2
2015-04-14 16:37:41: App name:child id:2 online
2015-04-14 16:37:41: Stopping app:child id:3
2015-04-14 16:37:41: App name:child id:3 disconnected
2015-04-14 16:37:41: App name:child id:3 exited with code SIGTERM
2015-04-14 16:37:41: Process with pid 12154 killed
2015-04-14 16:37:41: Starting execution sequence in -cluster mode- for app name:child id:3
2015-04-14 16:37:41: App name:child id:3 online

You are totally right. my mistake. for some reason i though it was the other way around.

any way to set time to wait until next restart?

4 processes running.

  • restart 1
  • wait 30 seconds
  • restart 2
  • wait 30 seconds
  • restart 3
  • wait 30 seconds
  • restart 4

and if restart 1 fails - stop and leave other 3 running.

while I have a warm-up period of 30 seconds before it starts to listen for connections.

have some problem with @liesislukas anyone help

any way to set time to wait until next restart?

4 processes running.

  • restart 1
  • wait 30 seconds
  • restart 2
  • wait 30 seconds
  • restart 3
  • wait 30 seconds
  • restart 4

and if restart 1 fails - stop and leave other 3 running.

while I have a warm-up period of 30 seconds before it starts to listen for connections.

exactly what i need if anyone knows how

Was this page helpful?
0 / 5 - 0 ratings