How to Kill a Process in Linux

Every Linux operating system comes with the kill command. The sole purpose of this tool is to terminate a target process. It’s a powerful tool that makes Linux quite versatile, especially in server and enterprise fields where a major change/update can take effect without restarting the entire machine. In this article, I’ll be showcasing how to kill a process using kill, pkill and killall.

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.

$ kill <signal_or_options> <PID(s)>
$ 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.

$ kill --help

It’s run from /usr/bin directory.

$ which kill

The benefit is, it also allows access to pkill, another command similar to kill that allows terminating process based on their name.

$ pkill --help

$ which pkill

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.

$ killall --help

$ which killall

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.

$ ps -A

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.

$ pidof <process_name>

Another interesting tool to grab the information about the target process is pgrep. It’s specifically designed for the purpose.

$ pgrep <option> <process_name>

Kill signals

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.

$ kill -l

There are 2 ways to define which signal you want to send. You can either use the full signal name or its equivalent value.

$ kill -<signal> <PID>

Or,

$ kill -<signal_value> <PID>

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.

$ killall -l

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.

$ kill <option> <PID>

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.

$ kill <option> <PID_1> <PID_2>

Let’s specify which signal you’d like to send to the target.

Want to terminate a process using its name only? Use pkill.

$ pkill <option> <process_name>

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.

$ killall <option> <process_name>

For example, when running Firefox, it starts a handful of processes. To kill all of them at once, run this command.

$ killall firefox

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.

$ killall -u <user>

Permission conflict

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.

$ kill yes

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.

Final thoughts

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.

$ man kill

$ man pkill

Enjoy!



from Linux Hint https://ift.tt/2HwtFNC

Post a Comment

0 Comments