Alex Mills Alex Mills - 1 year ago 160
Node.js Question

Getting id of worker from inside worker process

Using the node.js Cluster module, it is straightforward to get the id of a worker process.

that would be:

cluster.on('fork', function (worker) {
console.log('A worker',, 'was forked.');

but how can I get the
of the worker from inside the worker itself? How come the cluster module doesn't give the worker an id when the cluster forks the worker?

Do I really have to send the worker it's cluster id from the master process?

I am looking for something akin to:
(as opposed to


in any case, I am having trouble figuring out what the id of the worker is from inside the worker itself.

Answer Source
var cluster = require('cluster');

if (cluster.isMaster) {
  console.log('I am master');
} else if (cluster.isWorker) {
  console.log('I am worker #' +;

as in here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download