Kill Command in Linux

We offer you a brighter future with FREE online courses - Start Now!!

In this article, you will learn all there is to the kill command in Linux-based operating systems. We will be going through, what the kill command is, understanding its signal, and looking at the syntax and options of the kill command. In the end, we will also be looking at some practical examples of the kill command in action!

What is Linux kill command?

Kill is a command-line-based utility in Linux-based operating systems that helps terminate a process that is executing. It is builtin in most Bourne-based shells like bach and Zsh. But why use it?

Even though Linux is the greatest operating system in the world, it is not perfect, it has its own flaws. Every once in a while, some applications and programs may start behaving erratically. They become unresponsive or start consuming a lot of system resources.

The worst part of these erratic applications and programs is that you cannot restart them as the original program never shuts down completely. This leaves us with only 2 alternatives, reboot the entire system or kill the process of the application or program.

The kill command comes in handy in exactly these kinds of situations. There are many tools that allow you to terminate errant processes, and “kill” is one of them. The command behavior is slightly different between various shells and the standalone “/bin/kill” executable.

Syntax of the kill command in Linux

The syntax of the kill command is very easy:

kill <options> <signals> <PID>

Let us take a closer look at the fields in the syntax of the kill command:

1. <options>

This field takes in a range of options that specify how the kill command should work and print the output.

2. <signals>

This option takes in the value of the signals specified by the user.

3. <PID>

This field takes in the value of the process identity, you can also specify multiple PIDs in this field.

Options used with Linux kill command

In typical Linux fashion, even the kill command has options just like every single command in Linux. However, unlike most commands, the kill command has only 4 options (out of which 1 of them are not even considered an option!). Let us take a look at them:

1. -s<signal>

This option specifies the name, abbreviated name, or the number of the signal to be sent, preceded by a dash. Here are some examples: -SIGTERM, -TERM, or -15.

2. -l

This option lists all the available signal names (which we will look into in the next section). You can also write this option as “–list”

3. -L

This option lists the available signal names and numbers in a table. You can also write this option as “–table”.

4. –help

This option displays the help menu of the kill command which contains infomation abouts its options, usage and exit statuses as shown below:

help menu of the kill command

Signals of the kill command

The kill command sends a signal to the process identity (PID) you specified, causing them to act accordingly to the signal. Before we do deep into the topic of signals, let us first run the command “kill -l” to look at all the available signals:

signals used with the kill command

Let us first discuss the theory part of signals and later jump to practical topics like how to specify them.

There are many different signals that can be sent (64 to be precise), out of which most users are interested in only 3 – SIGTERM, SIGKILL, and SIGHUP. If you don’t specify anything in the <signal> field in the syntax, the kill command will take the default value like 15 or SIGTERM.

Programs that handle the SIGTERM signal can do useful cleanup operations before quitting like saving configuration information to a file. The process can intercept all signals except SIGKILL and SIGSTOP. All the available signals have different names and have corresponding numbers.

Note that the specific mapping between numbers and signals can vary between UNIX implementations.

Let us now look at the practical part of the signal, we saw that the 3 most used signals are SIGTERM, SIGKILL, and SIGHUP, let us take a closer look at them:

1. SIGHUP – 1 – Reload a process
2. SIGKILL – 9 – Kill a process
3. SIGTERM – 15 – terminate a process.

We can specify signals in the syntax of the kill command in 3 ways, let us look at each method:

1. Using the number

You can specify the corresponding number of a signal in 2 ways, the first is to simply put a hyphen before the number (like “-15”). The second method is to use the option “-s” followed by the signal number (like “-s 15”).

2. Using the name with the prefix “SIG”

Another method to specify signals is by using the full name of the signal including the prefix “SIG”. Just like we specified number in 2 ways, we can also specify the names in the exact same ways: you can either write “-SIGTERM” or “-s SIGTERM”

3. Using the name without the prefix “SIG”

If you are lazy and want to improve your efficiency, you can follow method 2, but remove the prefix “SIG”, meaning, you can now write it as either: “-TERM” or “-s TERM”.

PID of a process in Linux kill command

We all know by now that every process had a process identity number. Let us look at what happens when we pass different types of PIDs into the <PID> field in the syntax of the kill command:

1. The signal is sent to the process with an ID equal to the PID if the PID specified by the user is greater than 0.

2. The signal is sent to the process in the current process group if the PID specified by the user is equal to 0.

3. The signal is to all the processes with the same UID as the user invoking the command if the PID specified by the user is equal to -1.

4. The signal is sent to all the processes in the process group eq with GID equal to the non-negative value of the PID if the PID specified by the user is less than -1.

Now that we have laid down the basics, let us look at some cool examples of the kill command terminating commands in the terminal!

Actually, before we see the kill command in action we need to understand another command called “ps”. As we saw, we need the PID of a process to specify it in the syntax, but how do we know the PID of the command we want to terminate?

That is exactly what the ps command is for. We can simply be generic and type the standalone ps command and then kill the PID we want:

finding the pid of a process by using the ps command

Or you can use the “pidof” command to specify the process you want the PID of. For example, in the output shown below, the command “pidof firefox” displayed the IDs of all firefox processes.

finding the pid of a specific process by using the pidof command

Terminating a process using the kill command

Now that we have our PIDs, we can now terminate them using any one of the methods we discussed above to specify the signal:

kill -9 <PID>

terminating a process using the kill command

Terminating multiple processes using the kill command

If you want to terminate more than 1 process using the kill command, simply specify each PID one after the other separated by a space as shown:

kill <PID1> <PID2> <PID3> <PID4>

terminating multiple processes using the kill command

Terminating all processes of an application

We can also combine the kill command with other commands also, with and without piping. For example, to terminate all the processes of firefox, use the command shown below:

kill -9 $(pidof <application>)

terminating all processes of an application

Reloading process using the kill command

We saw that the SIGHUP signal tells the process to reload its settings. For example, to reload Nginx, we need to send a signal to the master process. The PID of the Nginx master is in the “nginx.pid” file, which is in the directory “var/run”. To obtain the PID of Nginx master, use the command shown in the screenshot below:

reloading process using the kill command

Once you obtain the PID, all we need to reload it is to use the signal in any of the 3 formats we discussed above:

kill -1 <PID>

Translating signal number

To translate any signal number into a signal name, use the option “-l” or “-list” as shown:

translating signal number

Easiest way of Killing processes

If you are really lazy and want to be more productive, you simply use the “killall” command as it kills every single command running in your system.

Summary

As you have seen, the kill command is a really simple command that helps in killing, reloading, termination and so many more operations on processes and applications. You have now learned what the kill command is, what it is used for, the syntax, options, signal, PIDs and practical examples of the kill command in the terminal.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *