This guide will show you how to kill a process on Linux using the process’s name.
There are several ways that you can kill/stop a process on a Linux based operating system.
One key way to terminate a process on Linux is to utilize its process ID. Every process on your system has an ID assigned to it when it starts. The downside is that you will need to know the ID of your process before you can kill it.
While it is relatively easy to get this ID using tools such as PS or a task manager such as the top command it is possible to avoid this altogether by using the name of your process.
The pkill command allows you to provide the name of a process. It will then work out the ID of all matching processes and terminate them. The one downside of this tool is that it will terminate all processes that use the specified name and not one program. This makes is significantly riskier to use a process name over a process ID.
In the following sections, we will walk you very quickly through how to kill a process on Linux using its name and even provide some quick examples of how to terminate a process.
Kill a Process by Name on Linux
At its most basic usage, all you need to do to kill a process by name on Linux is to use the pkill command followed by the process name.
By default, this command will match all processes that start with the specified service name. We will give an example of this shortly so that you understand exactly what you mean.
If you want pkill to only use an exact match, don’t worry; we will cover how to override this behavior in the next section.
pkill <PROCESSNAME>
For example, if you wanted to kill all processes on your Linux system using the name “firefox
” you would use the following command.
pkill will search the list of running processes and terminal any that contain the word “firefox
“.
pkill firefox
As mentioned earlier, by default, pkill will kill all processes even if they’re only partially matched. For example, we can still terminate our “firefox
” process by using “fire
” as the process name.
This shows the potential danger of the default behaviour of this command. You can accidentally end up killing processes that happen to be a partial match with the process name you specified.
pkill fire
Kill Process by Exact Name
As mentioned earlier, the problem with using pkill to kill a process by its name is that, by default, it performs a partial match. Luckily, there is an option that we can use that enables us to change the name matching behavior of pkill.
To change the pkill command to perform an exact match when killing a process by name, you will want to use the “-x
” option.
pkill -x <PROCESSNAME>
To give an example, let’s try to kill our Firefox process using the “fire
” name.
pkill -x fire
This time, you will notice that Linux wouldn’t have terminated the process. However, if we specify the exact name of the process we want to kill, Linux will terminate it.
pkill -x firefox
Conclusion
Hopefully, at this stage, you will understand how to kill a process on Linux by using its name.
Knowing how to kill a process using only its name can be super useful, as it avoids having to work out the ID of the processes that you want to terminate. Partial matches can be super useful when trying to stop a process where you only know what part of the name might be.
Please feel free to post a comment below if you have had any issues with killing a process by name.
If you found this quick guide to be helpful, be sure to explore our many other Linux guides.