Kill Command Examples

Warp Terminal

Found a stubborn background process eating up system resources?

You can kill it using the kill command 😉

But how do you use the kill command? Well, in this tutorial, I will walk you through the essentials needed to learn how to use the kill command:

  • The basic syntax and popular flags of the kill command
  • Practical examples of the kill command
  • Practice questions to get better at using the kill command

Here’s How to use the Kill command

To use the kill command to its full potential, it is important to know its syntax, so here’s a basic syntax of the kill command:

kill [options] <PID>

Here,

  • [options]: it is used to fine-tune the behavior of the command as per your needs, such as you can use the -s option to specify a kill signal.
  • <PID>: here’s where you specify the process ID of a target process to kill.

Now, let’s take a look at the list of the options available to you:

Option Description
-s Specify what kill signal to send.
-l Shows the name of the signal through the signal number.
-L List all the available signals.
q Sends the signal using sigqueue(3) instead of kill(2), allowing an additional integer value to be sent along with the signal.

The key option here is the -s flag, as you will mostly be using the kill command with specific kill signals in mind.

So here’s a list of common signals used with the kill command:

Signal Number Signal Name Description
1 SIGHUP Re-read configuration (like reloading web server settings).
2 SIGINT Interrupt the process, similar to pressing Ctrl+C.
3 SIGQUIT Quit and create a core dump (for debugging crashes).
9 SIGKILL Forcefully terminate immediately (use with caution!).
15 SIGTERM Politely request termination, allowing for cleanup (default).

If you don’t specify any signal number, it will use SIGTERM by default.

Now, let’s take a look at some practical examples of the kill command.

Practical examples of the kill command

In this section, I walk you through practical examples of the command, so you can have a better idea of how to use the kill command on Linux:

1. List available signals

To list available signals, all you have to do is use the -L flag with the kill command, as shown here:

kill -L
List available options with the kill command

As you can see, the above response repeats the termination signals. So how do you get the list of every signal available without repetition?

Easy, you can use the standalone version of the kill command by executing in the following manner:

/usr/bin/kill -L
List available kill signals

2. Kill a process using the kill command

To kill a process, you require a PID (Process ID) to specify the target process. To find the PID of a process, you can use the pidof command as shown here:

pidof <process_name>

For example, if I want to find the PID of the Firefox browser, then I use the following:

pidof firefox-bin
Find the PID of a process

Once you have a PID, you can use the following command:

kill <PID>

As I mentioned earlier, if you don’t pass any termination signal, it will use SIGTERM, enough to kill most processes.

If the process is super stubborn (and has numerous child processes associated with it) and does not go away by the default signal, then you can use the SIGKILL.

🚧

The SIGKILL signal kills the child processes as well so use it with caution ⚠️.

Here’s how you can use SIGKILL to kill a stubborn process:

kill -9 <PID>

For example, the Spotify client was unresponsive and wasn’t working as expected, so here’s how I used SIGKILL to kill Spotify:

kill -9 13234

Here, the 13234 is the PID of the Spotify desktop.

If you’re curious to learn the difference between SIGTERM (default) and SIGKILL then you can refer to our detailed guide on that matter:

What is SIGTERM? What’s the difference between SIGKILL & SIGTERM?Both SIGTERM and SIGKILL are used for killing a process in Linux. But you should prefer using SIGTERM. Here’s why!

3. Kill multiple processes at once

To kill multiple processes, all you have to do is append multiple PIDs to the kill command separated by spaces:

kill [options] PID1 PID2 PID3 PIDN

For example, here, I used the PID of Firefox and Spotify to kill both of them at once:

kill 10793 9758

4. Find the name of the signal through the number

Typically, you will stumble upon a number instead of the name of the kill signal, and you might want to know what signal was used there.

For that purpose, you can use the -l flag appending the number of the signal as shown here:

kill -l <Signal_number>

For example, if I want to know what is the name of the termination signal 9, then I will use the following:

kill -l 9
Find the name of the termination signal with the kill command

If you notice the above output, then you realize that it only printed the KILL instead of SIGKILL.

Why do you ask? Well, it won’t add SIG before the name of any termination signal.

Suggested Read 📖

7 System Monitoring Tools for Linux That are Better Than TopTop command is good but there are better alternatives. Take a look at these system monitoring tools that are similar to top, but better than it.

Practice questions 🗒️

You must practice the commands to get better at them, and quickly use them when needed in day to day life.

So here I’ll be sharing some practice questions for the kill command:

  1. Find the PID of your default browser and then kill it using the SIGKILL.
  2. How can you hold the process instead of killing it?
  3. What is an interactive way of sending kill signals (Hint: you can use htop)
  4. What is a zombie process and how to kill it? (Hint: guide to zombie process)

If you discover any difficulties solving the above problems, reach out to us through the comments or post your query in our community forum.

Explore How You Can Use Different Kill Signals

If you are curious to learn the meaning of different kill signals, refer to our detailed guide on using different termination signals on Linux:

How to use SIGINT and other Termination Signals in LinuxTerminating executing process is more than just kill -9. Here are some of the prominent termination signals and their usage.

Moreover, if you are new to Linux, I suggest you to also go through our command tutorial for beginners.

Linux Command Tutorials for Absolute BeginnersNever used Linux commands before? No worries. This tutorial series is for absolute beginners to the Linux terminal.

💬 How often do you find yourself using the kill command? Or do you prefer the GUI?

Discover more from 2threads

Subscribe now to keep reading and get access to the full archive.

Continue reading