kill Command in Linux Explained

Written by: Bobbin Zachariah   |   Last updated: August 15, 2022

Being able to recognize when and how to terminate running processes is an essential skill for system administrators. Sometimes a process becomes stuck and only requires a gentle nudge to either restart or stop. Sometimes a process takes up all the system resources. In both situations, you require a command that will let you manage a process. 

The Linux operating system has a number of commands that can be used to end errant processes (rogue processes), such as pkill command, kill command, killall command, and so on.

In this tutorial, we learn about the kill command in Linux.

Understanding Linux signals

Signals are used by Linux processes to communicate with one another. A process signal is a preset message that processes understand and can either ignore or respond to. Developers define how a process handles signals.

Below are some of the Linux signals that processes can receive and respond to:

SignalNameDescription
1HUPHang up.
2INTInterrupt.
3QUITStop running.
9KILLUnconditionally terminate a process.
11SEGVSegment violation.
15SIGTERMGracefully terminate a process.
17STOPStop unconditionally but don't terminate.
18TSTOPStop or pause but continue to run in the background.
19CONTResume or continue execution after STOP or TSTP.

kill command in Linux

Most shells, including Bash and zsh, come with built-in kill commands. The  /bin/kill or /usr/bin/kill executable and the shells built-in kill have slightly different behavior. To see all the locations on your system that contain kill, use the type command and the -a option:

type -a kill

You can see from the result above that when you type kill on your command line, the shell builtin is used rather than the executable binaries. To use the executable binary use the complete path to the binary (for example, /bin/kill or /usr/bin/kill).

We will discuss the bash built-in command in this article, so if you are using another shell, you must switch to bash to get similar results.

The kill command has the following syntax:

kill [options] <pid> [...]

The kill command lets you send signals to processes depending on their PIDs. All of the PIDs given on the command line receive a SIGTERM signal by default when the kill command is used.

The following are the most commonly used signals:

  • 1 (SIGHUP) - Hangs up the process.
  • 9 (SIGKILL) - Unconditionally terminates the process.
  • 15 (SIGTERM) - Stop a running process gracefully.

To list all the available signals that can be sent with the kill command use the kill command together with the -l option:

kill -l
linux signals

To send a process signal, you must be the process owner or logged in as the root user. Regular users are only able to send signals to their own processes. To send signals to the process of other users you must be a root user.

The -s option allows you to specify which process signals to send by using their name or signal number. Below are several ways you can use to specify signals:

  • Using the equivalent signal number (-1 or -s 1).
  • Prefixing the signal name with “SIG”(-SIGHUP or -s SIGHUP).
  • Omitting the “SIG” prefix (-HUP or -s HUP).

So go ahead and pick whichever option works best for you. Here are some examples of how to use the kill command to terminate a process:

Note that:

  • The process groups ID is indicated by negative PID values. All processes contained in a certain process group will get the signal if a process group ID is passed.
  • A PID of -1 is particularly unique because it represents all processes on the system, with the exception of the init process, which is the parent process of all other processes on the system.

Find Linux process information

A process is what a program is when it runs on the system. To have a peek at these processes, you must first familiarize yourself with ps. This command can show a large amount of data about all of the programs that are currently running on your system.

Here is a basic syntax of ps:

ps
Linux process information

As you can see, the default ps doesn't offer a lot of details. By default, the command only shows processes that are currently running on the current terminal and that are owned by the current user. In this instance, all that was executing was the bash shell, zsh shell and the ps command itself.

The basic output displays the application's process ID (PID), the terminal (TTY) from which they are being executed, and the amount of CPU time each process has consumed. You can combine the ps combine with several options to view more information about the programs.

If you want to learn more about the various options available with ps, simply type 'man ps' into your terminal.

ps has one downside, despite being excellent for learning about the processes that are active on the system. Only information for a particular period of time can be seen with this command.

The ps command makes it difficult to identify patterns in processes that are regularly switched in and out of memory. Instead, the top command comes in handy. Similar to the ps, the top command shows process information, but it does so in real-time. Here is an example of the top showing processes in real-time.

kill command options

Useful kill command options are:

OptionsDescription
-sUsed to specify to the signal name.
-nUsed to specify the signal name
-llist the signal names
-Lsynonym for -l

How to kill the process in Linux

To use the kill command to terminate a process, you must first get its process ID (PID). Various commands, including ps, pgrep, and top (or other top variants, such as htop, btop++, etc.), and pidof can be used to accomplish this.

Now that you are familiar with signals and how to locate processes on your system, let's now use the kill command to kill a process.

Kill a process using processes ID

Let's say you need to end the vscode process because it has stopped responding. The pgrep command can be used to find its PIDs. The pgrep command is used to find the process id of a specific process.

pgrep vscode

This prints the process IDs of all the vscode processes:

Output
4545 4345 2345

You can now then kill all processes once you know their PIDs by sending a KILL signal (SIGKILL or -9) :

kill -9 4545 4345 2345

Here the kill command sends the kill signals to all the processes that belong to the vscode program.

Kill a process using the process name

The kill command has a significant drawback in that it does not allow you to kill processes by name. The pkill command is a great tool for stopping processes by using their names rather than their PID numbers. But fortunately, for the kill command, there is a workaround by combining the above command with the kill command.

kill -9 $(pgrep vscode)

You can also achieve the same thing with pidoff as shown:

kill -9 $(pidof vscode)

This way the kill command will send the specified signal to all vscode processes.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment