1

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
4

1 回答 1

5

我建议您不要监控集群中的主进程,因为您将无法处理SIGKILL. 您可以为此使用upstartmonitforever

关于第二个问题,您可以设置流程标题:

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

有关更多信息,请参阅文档

于 2013-09-06T05:44:17.600 回答