Command Linux

Reasons Why Kill May Not Work in Linux, How to Resolve It?

All of us have, at one point or another, experienced a program freezing and not responding. In such cases, terminating the program and restarting it is a simple and efficient solution. In this article, we will show you how you can terminate unresponsive processes using the Kill command.

The Kill command

As mentioned earlier, when a program stops responding, and you cannot restart it, you need a new solution. At that point, Windows users would open their Task Manager to get rid of the frozen process. There is no built-in Task Manager in Linux, but there is a command that we can use to achieve the same effect as the End Task functionality in Windows. This command is known as the Kill command, and we can use it in multiple ways to assist us and the system in getting rid of corrupt instances.

Kill Syntax

The syntax for the Kill command is a straightforward one.

$ kill [signal] PID

Here, two things need explanation – namely, signal and PID. Let’s look at both one by one and learn how they contribute to the functionality of this command.

The Signal parameter

First is the signal. Signals are simple digits that represent a name. The Kill command can perform different tasks depending on the signal you provide. Users can use more than 60 different values for signals, all of which perform separate, specific tasks. To view all of these options, you can use the following command:

$ Kill -l

When you use this command on Ubuntu’s command terminal, you get this on your screen:

As you can see, there are a total of 64 options that you can select from.

Although there are more than 60 different signals, two of the most common ones are described below.

  1. SIGKILL (9): As the name suggests, this command Kills the process completely without saving the program data. This command is only to be used as a last resort.
  2. SIGTERM (15): This generates a termination signal which closes the program in a safety-first way. This is also the default signal.

The signals can be further specified in 3 different ways described below.

1. We can specify the signal with the respective digit:

$ kill -9 PID

2. We can specify the signal with its name:

$ kill -SIGKILL PID

3. We can specify the signal without SIG in the name.

$ kill -kill PID

All of these commands perform the same task.

The PID parameter

PID is the Process Identification number. In Linux and Unix-like systems, every process carried out by the system is given a Process Identification number (referred to as PID hereinafter). To kill a process, one must know its PID. To find the PID for a given process, we can use the following commands.

$ pidof [process name]

$ pgrep [process name]

When you use these two commands in Ubuntu’s command terminal, you get the following result.

Using the Kill command

Now that you have understood the syntax, we can execute the Kill command. For the sake of example, we will be making use of Kill on Thunderbird Mail – a preinstalled program in Ubuntu. We will first identify its PID and then end the process using the SIGTERM signal.

First, open the command terminal for Ubuntu. Once done, run the following command:

$ pgrep thunderbird

You should get an output similar to the one we got in the image below:

As you now know, the PID of Thunderbird Mail runs the following command to end the process.

$ kill -SIGTERM 7075

Or

$ kill -15 7075

When you press enter, you will notice that the process and its window no longer exist. You can also end multiple processes together by simultaneously entering the PIDs of the processes.

$ kill[signal] PID1 PID2…..PIDN

Moreover, users can terminate a process by using both the kill and pidof/pgrep commands in the same line. The syntax for such an arrangement is:

$ kill[signal] $pgrep [process name]

Reasons why Kill may not work

As stated earlier, the default signal sent with the KILL command is SIGTERM. SIGTERM essentially notifies the process that it should clean itself up and be terminated. This is the “nice” way of terminating or killing a process. However, with some processes, it is possible that the nice way doesn’t cut it.

If you notice that the process you had commanded to end is still running after using the SIGTERM signal, worry not, as this is a common complication that occurs while using Kill. As we said, this can occasionally happen and with certain processes.

To get rid of this problem, you can use the SIGKILL signal to terminate the process completely. The SIGKILL signal enables the system to take matters into its own hands. So, if you meet such a sticky situation, you can always come out of it using the SIGKILL or -9 signal.

It is important that you use SIGKILL sparingly and when presented with a situation that SIGTERM can not solve. SIGTERM remains the default and the safest; hence, the best way to end a process.

The Killall command

The Kill command is native to both Linux and other Unix-based operating systems, but the Killall command is for Linux-only users. This command enables the user to terminate a process by using the name of the process. There is no need to find the PID of the process. The syntax for the Killall command is:

$ killall[signal] [process name]

This command can serve as a nice alternative if, for some reason, Kill does not work for you even after the prescribed solutions.

Conclusion

This article learned about the Kill command, its purpose, syntax, parameters, and usage. Furthermore, we saw what may cause the Kill command not to function properly and fix these issues. Hopefully, this has been an informative and interesting read for you, and we resolved any issues you were having with this command.

Related Articles

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Back to top button