In my effort to make helpful posts on node.js, I have been working with the child_process module lately. This is a very useful module that can be used to launch a command on the operating system of the computer that the node.js project is running on, including node itself. So it is a way in which I can run another script from within a script and when doing so this child process will end up having it’s own process id, and is a way to go about doing some things in parallel in a node.js environment.
When launching such a process there might be some kind of condition in which I will want to kill the child process if it is the kind of process that will keep going otherwise. Luckly doing so is very easy I just need to use the kill method that is provided to the object that is returned when using spawn.
In this demo I will be using two scripts one is the usual index.js file, and the other I will be calling loop.js. The main index.js file will launch the loop.js file using the spawn method to call node, at which point loop.js will keep looping, logging to the console until it is killed by a condition in the main index.js file.
So the loop.js is a very simple script that will just log to the console every half second by making use of the setInterval core js method.
So in this demo loop.js does not do much of anything, but it could be some other kind of script that does some heavy lifting of some kind.
The main index.js file which I will be starting with node in the command line serves as a main process that will start the child process, and when needed, stop it as well.
So the spawn method will call node in the command line, and pass a single argument, which is the same as calling:
When called it will return an object, that I have referenced in the variable script. This object has methods that allow for me to define event handlers to work with the output of the process, and also to stop it by calling the kill method of this object.