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
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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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
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
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
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
As you can imagine, you should use a lot of caution with the
pkillcommand because you could easily kill a process that you didn’t intend. For instance, if we had another script
example2.shrunning, 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
pgrepcommand to get a preview of how many processes
pgrep to determine which processes have the name:
$ pgrep example 17555 17557 17559
pkill example would kill three processes.
Note that the
pkill commands will accept most of the same options as the regular
kill command. For example, a common option specified with
-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
In this tutorial, we saw how to we saw how to kill a process by name with the
pkillcommands 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
killcommand. Still, the
killcommands 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.