Killing a child process in Node.js

When working with child processes in Node.js, there might be cases where you need to terminate or kill a child process programmatically. Killing a child process can be useful in situations where the process is taking too long to complete, consuming excessive resources, or needs to be stopped for any other reason.

In this blog post, we will explore different methods to kill a child process in Node.js.

Table of Contents

Using the kill() Method

Node.js provides a built-in method kill() on the child process object to send the SIGTERM signal to the child process, which in turn gracefully terminates the process. The kill() method takes two arguments: the signal to send to the child process and an optional callback function.

const { exec } = require('child_process');

// Spawn a child process
const child = exec('node myChildProcess.js');

// Terminate the child process
child.kill();

In the above code snippet, we create a child process using exec() and then use the kill() method to terminate it. By calling kill() without any arguments, we send the default SIGTERM signal to the child process.

Sending a Signal to the Child Process

Apart from the SIGTERM signal, Node.js provides various other signals that can be sent to a child process. These signals can be used to perform different actions, such as forcefully terminating the process or sending a specific message to the process.

To send a signal other than the default SIGTERM, you can pass the signal as the first argument to the kill() method.

const { spawn } = require('child_process');

// Spawn a child process
const child = spawn('node', ['myChildProcess.js']);

// Send a different signal to the child process
child.kill('SIGINT');

In the above example, we use spawn() to create the child process and then use kill() to send the SIGINT signal to it. SIGINT is commonly used to interrupt a process, similar to pressing Ctrl+C in the terminal.

Using the kill() Method with Different Signals

As mentioned earlier, there are various signals available in Node.js that can be sent to a child process. Some commonly used signals are:

Here’s an example of using the kill() method to send different signals to the child process:

const { spawn } = require('child_process');

// Spawn a child process
const child = spawn('node', ['myChildProcess.js']);

// Send SIGINT signal
child.kill('SIGINT');

// Wait for 2 seconds and then send SIGTERM signal
setTimeout(() => {
  child.kill('SIGTERM');
}, 2000);

In the above code snippet, we first send the SIGINT signal to interrupt the process, and then after a timeout of 2 seconds, we send the SIGTERM signal to gracefully terminate the process.

Conclusion

Killing a child process in Node.js can be achieved using the kill() method on the child process object. By sending different signals, you can perform actions such as gracefully terminating the process, interrupting the process, or forceful termination.

Remember to handle errors accordingly and ensure proper shutdown of child processes to avoid any memory leaks or resource wastage.