0

I am creating a library that will start a process which then forks child_processes; it's much like using the cluster module with a server, except in my case neither the parent/master nor the child_processes are servers.

What I need to do is be able to give the user the option of killing the process once the whole thing starts. The only way I know of doing this, is to have the parent process listen on a specific port. Then I can communicate with the parent process, and it can be responsible for killing the child_processes.

The one problem with this, is if the user runs two or more different process groups side-by-side, then I would have to increment the port that each one listens on, and then the user doesn't actually know what the ports are anymore.

Does anyone know about the problem I am trying to solve and is there a good solution for this situation?

If I end up listening on a specific port, should I use the net module from core Node? Probably the net.Socket() option https://nodejs.org/api/net.html#net_class_net_socket

Seems like I need to make this process into a server so that it can listen on a port and easily receive messages.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

0

Just launch with child_process module and the child processes will terminate when the parent does.

Then send the parent a signal like SIGINT or SIGHUP or SIGKILL. See https://nodejs.org/api/process.html#process_signal_events

// Start reading from stdin so we don't exit.
process.stdin.resume();

process.on('SIGINT', () => {
  console.log('Got SIGINT.  Press Control-D to exit.');
});
Jason Livesay
  • 6,317
  • 3
  • 25
  • 31