Killing a process
For killing a process, we’ll be using a handful of tools: kill, pkill, and killall. All of them work basically the same way.
These tools don’t terminate the process themselves. Instead, they send a designated signal to the target process or process groups. If you didn’t specify any specific signal, SIGTERM is sent as the default signal. However, there are a number of supported signals, for example, SIGKILL, SIGHUP etc.
Here’s the basic command structure for kill, pkill and killall.
$ pkill <signal_or_options> <process_name>
$ killall <option> <process_name>
Whenever possible, it’s recommended to use kill.
Kill, pkill and killall locations
Kill is the default command for terminating a process.
It’s run from /usr/bin directory.
The benefit is, it also allows access to pkill, another command similar to kill that allows terminating process based on their name.
Some apps run multiple processes of the same executable. If you want to terminate a number of processes with the same name, use the killall tool.
Listing all the running processes
The very first task is identifying the PID (process identification number) and/or the process name that you’d like to terminate. For this example, I’ll be using Firefox as the target process to terminate. Run the following command to list all the running processes on the system.
For most of the tasks, we need to know the PID of the target process. However, in certain situations, using the process name is more suitable.
If you know the exact name of the target process, you can directly get the PID using pidof.
Another interesting tool to grab the information about the target process is pgrep. It’s specifically designed for the purpose.
Now, let’s have a look at the signals that the kill tools support. It’s a huge list. Of course, not all of them are necessary for every single situation. In fact, most of the situations require only a handful of signals.
First, let’s take a look at the list that kill supports.
There are 2 ways to define which signal you want to send. You can either use the full signal name or its equivalent value.
The most popular signals are SIGHUP (1), SIGKILL (9) and SIGTERM (15). Generally, SIGTERM is the default and safest way to terminate a target process.
In the case of pkill, the supported signal is the same as kill. However, in the case of killall, the number of supported signals and the signal names are different.
Killing a process
To kill a process, we need the PID of that target process. Assuming that you have the PID, run the following command to kill it.
Here, kill will send the default signal SIGTERM to the PID(s). If you wanted to terminate multiple processes, then mention all the PIDs separated by space.
Let’s specify which signal you’d like to send to the target.
Want to terminate a process using its name only? Use pkill.
In some cases, a particular application may have too many processes running. Typing all those PIDs is time-consuming and tiresome. In such scenarios, we’ll be using the killall tool. It’s quite similar to kill but it works with process name.
For example, when running Firefox, it starts a handful of processes. To kill all of them at once, run this command.
Want to terminate all the processes that are running under a certain user? Killall can do the job, no problem. Be careful when running this one as it might break down the system and create other issues. It won’t work if you’re trying to terminate processes that are running under a different user with higher privilege.
The inherent characteristics of the Linux user hierarchy also apply when you’re about to terminate an application. A user can’t terminate processes that are running with higher privilege, only a processes with equal/lower privilege. Moreover, a user can’t manipulate processes that are running under different user.
For example, let’s consider the yes command. If it’s called as the current user, it can be easily terminated using kill.
Now, what if yes was running under root? Calling kill as the current user won’t work.
Similarly, if a process was running under another user, you can’t terminate it from a different user account.
In this article, only the basics and common usage of these commands were showcased. These kill tools are capable of more than that. To have in-depth knowledge of the abilities of any tool, I recommend checking out the man page.