fawad
fawad

Reputation: 141

How to get the pid of forked child process NODE JS

I'm forking child process in node and my business logic requires me to call this fork process again and again.Issue is to clean up previous forked process before calling next one.Is there any way that i can get my forked process pid ,so we can store it and kill it on next run

 const program = path.join(__dirname, 'test.js');
  var myProgram= fork.fork(program, [], {
  silent: true
});

Upvotes: 0

Views: 1304

Answers (1)

Dan Starns
Dan Starns

Reputation: 3825

fork will retrun a ChildProcess object. This object contains a bunch of properties including pid and kill.

You can use the kill method to conveniently 'kill' your subprocess

  const forked = child_process.fork("path");

  forked.pid // 189...
  forked.killed // false

  forked.kill();

  forked.killed // true

Upvotes: 2

Related Questions