Nam Nguyen
Nam Nguyen

Reputation: 5750

Node.js cluster master process reboot after got kill & pgrep?

1st question:

In Node.js cluster, I know that if a cluster worker is crashed or got kill, it will auto boot up again with a new instance. But what if when master process got kill, how do we make master process auto boot up again if it got killed or crashed? I tried to kill master process and the entire cluster is killed as well. Is there anyway to prevent this?

if (cluster.isMaster) {
  require('os').cpus().forEach(function () {
    cluster.fork();
  });

  cluster.on('exit', function (worker, code, signal) {
    cluster.fork();
  });
} else if (cluster.isWorker) {
  ...
}

2nd question:

I figured we can find the master process from process.pid. But without this info from Node.js and looking at the output of pgrep -l node, how do I figure out which one is master process id? Is there anyway that I can tag or name the processes when I fork() it so I can find master process in pgrep command?

$ pgrep -l node
1420 node
1419 node
1418 node
1417 node
1416 node
1415 node
1414 node
1413 node
1412 node

UPDATED

After I apply process.title as @fardjad has suggested below, it works beautifully. Here is the results of pgrep:

  $ pgrep -fl acl
  1994 acl_api_worker_8 USER=nam
  1993 acl_api_worker_7 USER=nam
  1992 acl_api_worker_6 USER=nam
  1991 acl_api_worker_5 USER=nam
  1990 acl_api_worker_4 USER=nam
  1989 acl_api_worker_3 USER=nam
  1988 acl_api_worker_2 USER=nam
  1987 acl_api_worker_1 USER=nam
  1986 acl_api_master SHELL=/bin/bash

Upvotes: 1

Views: 1023

Answers (1)

fardjad
fardjad

Reputation: 20404

I suggest you not to monitor the master process in your cluster, because you won't be able to handle SIGKILL. You can use upstart, monit or forever for this.

Regarding to your second question, you can set the process title:

if (cluster.isMaster)
    process.title = 'master';
    ...
}

See docs for more info.

Upvotes: 5

Related Questions