How to kill a process by name

When you need to quickly or forcibly close a running process on a Linux system, many users will try to determine the process ID and then kill a process by its ID. While this works fine, it is sometimes easier or more convenient to kill a process by name. This way, we get to skip the step of looking up the process ID, and let our terminal do the work for us.

In this tutorial, you will learn how to kill a process by name on a Linux system. This is facilitated by the pkill and killall commands, which accept process names as an argument instead of a process ID number.

In this tutorial you will learn:

  • How to kill a process by name with killall and pkill
How to kill a process by name
How to kill a process by name
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software pkill, killall
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to kill a process by name




There are two default commands on Linux that can kill a process by name: killall and pkill. Although both commands accomplish the same thing, they both go about it a little differently.

First, let’s look at killall. We need to specify the exact name of the process that we want to kill. Let’s look at some examples of how it works by killing a few instances of the same Bash script with just one command.

$ killall example.sh
Using killall command to kill process by name
Using killall command to kill process by name

In this case, it’s definitely a lot easier to kill all these processes with a single command than having to specify each PID with kill. However, killall doesn’t discriminate and targets all instances of our script in the example. If we had only wanted to kill, say, two of them, then we’d still have to resort to using the kill command.

The other command we could have used is pkill. This differs from killall by not requiring us to specify the exact name of a process. So, using our previous example, we could kill all three processes of example.sh with a command like this:

$ pkill examp




Using pkill command to kill a process by name or pattern
Using pkill command to kill a process by name or pattern

WARNING
As you can imagine, you should use a lot of caution with the pkill command because you could easily kill a process that you didn’t intend. For instance, if we had another script example2.sh running, the previous command would’ve also terminated it. Sometimes this may be a good thing, but just be aware that the pattern matching can sometimes extend to more processes than you realize. You could always use the pgrep command to get a preview of how many processes pkill would terminate.

Using pgrep to determine which processes have the name:

$ pgrep example
17555
17557
17559

Thus, pkill example would kill three processes.

Note that the killall and pkill commands will accept most of the same options as the regular kill command. For example, a common option specified with kill is -9 to send a SIGKILL signal to a process. The syntax works the same on the other two commands. See the example below.

$ kill -9 1234
$ killall -9 example.sh
$ pkill -9 example.sh

Closing Thoughts




In this tutorial, we saw how to we saw how to kill a process by name with the killall and pkill commands on a Linux system. Each of these commands come with their own extensive list of options, many of which overlap with each other or are based off the kill command. Still, the killall, pkill, and kill commands have their own niches that they fill and it’s helpful to have all three in your Linux admin tool belt. Check out the man pages if you want to get a feel for their more advanced usage.