How to see list of child processes created by Fork and Spawn in nodejs ?
Asked
Active
Viewed 5,365 times
6

vijay
- 10,276
- 11
- 64
- 79
-
Nodejs does not have a built-in way to do this. You would have to use various OS tools or 3rd party NPM libraries for doing this. For OS tools, you can either run commands at the command prompt yourself to manually list them or you can run a child_process that issues the process listing command (for your OS) and then capture that output into your node.js app. – jfriend00 Jul 05 '19 at 07:59
1 Answers
3
You can either create a list of ChildProcesses, since spawn and fork return ChildProcess you can append a new item to the list just after creating it.
Second option would be to execute a command like ps and parse it's output. 1st option seems better for me, because it will be os independent and you will avoid parsing ps
output manually. Also you will have the actuall ChildProcess class which could be usefull.

cymruu
- 2,808
- 2
- 11
- 23
-
What should one do if one wants to end child_processes that have been create on anotehr nodeJS instance? – B''H Bi'ezras -- Boruch Hashem Apr 20 '20 at 07:09
-
@bluejayke I'd probably emit a signal to process from parent and handle it child process https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal or https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback – cymruu Apr 20 '20 at 07:18
-
1but how does one get access to the child process if it wasn't created in that nodejs instance? Meaning if its already running from somewhere else and the ChildProcess instance isn't available in the current code? – B''H Bi'ezras -- Boruch Hashem Apr 20 '20 at 07:19
-
@bluejayke im afraid you will have to setup some kind of communication between processess, or easier solution - create some kind of a manager or a server which will have knowledge of all running processes – cymruu Apr 20 '20 at 09:11