1. Overview

Sometimes, processes hang and we have to manually terminate them. We can do this by running the kill command.

In this tutorial, we’ll take a look at four different ways we can do it.

Note that while all commands mentioned in this tutorial were tested in Bash, they should be available in every POSIX-compatible shell.

2. PIDs and Signals

2.1. Signals

To kill a process, we send it an appropriate signal. Signal processing is critical to the Linux OS. With signals, we can tell processes what to do.

In this tutorial, we focus on terminating a running process, but signals might also be used to pause or continue. Check the signal(7) man page for the complete list of signals that Linux supports.

2.2. PID

To send a signal to a process with kill, we need the process’s identifier, or PID, for short. The PID of a process can be obtained by, for example, ps or top, but such is out of scope for this tutorial.

For the remaining sections, let’s assume that we have a hanging process with PID 123. Note that, while we’ll be using a single PID in our examples, we can always send the same signal to more than one process by supplying a space-separated string of PIDs.

3. SIGTERM (15)

The default behavior of kill is to send the SIGTERM signal to a process, by which we ask that process to gracefully shutdown.

As it’s the default behavior, we can call kill the process by simply providing its PID:

kill 123

To explicitly send the SIGTERM signal we use:

kill -s SIGTERM 123

We can shorten it further by using the signal id:

kill -15 123

In the following examples, we’ll be using this short format so we can get familiar with the numerical values.

4. SIGQUIT (3)

Sending a process the SIGQUIT signal is the same as asking it to shutdown with SIGTERM. The difference is that SIGQUIT makes the OS perform what is called a core dump:

kill -3 123

The core dump is a snapshot of the working memory of the process at the time we sent the kill signal and by default will be written to the current working directory.

We can use core dumps for debugging purposes.

Note that while quitting is the default behavior, Java is an example of a process that doesn’t quit with a SIGQUIT, it only does a core dump.

5. SIGKILL (9)

By choice of the programmer, processes don’t have to respond to every signal.

In such a case, or for processes that are hogging CPU for example, we can force it to terminate using the SIGKILL signal:

kill -9 123

With SIGKILL we ask the kernel to immediately shut down the process. The process dies and won’t clean up after itself.

This means there is a risk of data loss or even worse, data corruption.

Now, while SIGTERM, SIGQUIT and SIGKILL are certainly the most common, we’ve got one more signal to look at: SIGSTOP.

6. SIGSTOP (19)

Unlike the name might suggest, the SIGSTOP signal will not terminate a process. It is the equivalent of stopping and putting a process in the background with ctrl+z.

We stop a program with:

kill -19 123

The process can be resumed by sending the continue signal SIGCONT (18):

kill -18 123

7. Conclusion

In this article, we saw how we can use the kill command to terminate processes. We can terminate processes in various ways by sending different signals.

We should always first try to terminate a hanging process by sending either the SIGTERM or SIGQUIT signal. That way, the process can exit properly by cleaning up after itself. We should use SIGKILL as a last resort, as it may result in possible data corruption.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.