0

I want to get current process id which is currently used.I'm using cluster for multiprocessing. I need this because I want to kill child process after the use to reduce memory utilise but I only get the current process Id when I call process.exit() method. But I don't want to call process.exit() method in each route and cluster.on('exit') couldn't called.

omkar p
  • 43
  • 1
  • 8

3 Answers3

1

Process PID can be accessed by process.pid.

sptm
  • 311
  • 2
  • 9
1

Node has several global methods and attributes, one of them being process which holds the current node process info.

enter image description here

MrFrenzoid
  • 1,208
  • 6
  • 21
0

built-in/core modules process does this :-

const process = require('process'); // On ubuntu, you dont need this (if there is an error)

if (process.pid) {
  console.log('This process is your pid ' + process.pid);
}

Another answer from stackoverflow :- NODEJS process info

Muhammed Rahif
  • 434
  • 1
  • 5
  • 17
  • I've used this but this will work when I start server but when I call any route its not working . so is there any way to get process id all time when I hit endpoints. – omkar p Jan 05 '22 at 07:05
  • so you are saying that you can't access the `process.pid` in your router ?! – Muhammed Rahif Jan 05 '22 at 07:14
  • I can but I have to kill current process which are used by route and I don't want to add `process.exit()` method in route to call `cluster.on('exit')` method and I want to kill current process id. so do you have any example for cluster implementation with route. my question is related to cluster sorry for title. – omkar p Jan 05 '22 at 07:27