How to kill a process on a Linux system is an essential thing for admins and users to know. The go-to method for this is usually with the kill
command, which involves killing a process by its PID (process ID).
Sometimes, though, it’s more convenient to kill a process by name rather than going through the routine of locating its PID each time. There are two commands we can use to kill a process by name, those being killall and pkill.
In this tutorial, we’ll go over both killall
and pkill
commands and show examples for how they can be used to kill processes by name only.
In this tutorial you will learn:
- How to kill a process by name with killall and pkill
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 |
Kill process by name with killall and pkill
The first thing you’re probably wondering is, how does killall differ from kill?. There are two key differences. First, killall accepts a process name as an argument rather than PID. And the other difference is that killall will, as the name implies, kill all instances of a named process. Contrast this to the regular kill
command which only ends the processes you explicitly specify.
Unlike pkill, killall requires that you specify the exact name of a process. 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 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
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.
$ 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
Conclusion
In this guide, we saw how to kill a process by name with the killall
and pkill
commands. 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.
$ man killall $ man pkill $ man kill