How to Kill Process by Name in Linux

Last updated on August 14th, 2024 at 09:20 am

Linux provides many tools to manage system processes. System administrators use them to create, clone, and even destroy processes. Often they identify the PID of a process and then kill it using kill command. But sometimes you may need to kill all processes by name in Linux. There are multiple ways to do this in Linux. You can use any of pkill, pgrep, pidof and killall functions. In this article we will look at them in detail.

Why Kill Process By Name

Sometimes certain processes stop working or become unresponsive. In such cases, it is required to kill the process. These processes may also be running in background even after they have been closed. They occupy unnecessary system resources and slow down your system. Therefore, you need to kill them. It can be tedious to kill a process by finding its PID and then issuing kill command for it. If your system itself is slow, why run two commands to do a single task? It is much better and easier to kill a process by using its name.

How to Kill Process by Name in Linux

Here are the different ways to kill process by name in linux

1. Using pkill

pkill command allows you to directly kill process by name. It is similar to killall command but provides more options for pattern matching. For example, here’s the command to kill all processes by name apache.

$ sudo pkill apache

You may also use -f option to search the actual command used for running the process, instead of searching only the process name. For example, here’s the command to search and kill process with command /etc/apache/bin/apache

$ sudo pkill -f /etc/apache/bin/apache

The above commands will kill all processes owned by all users. If you only want to only kill processes owned by specific user, use -u ‘username’ option.

Like kill command, you can also send specific signals to processes by using -s option followed by signal number.

Bonus Read : How to create tar.gz file in Linux

2. Using pgrep

pgrep command returns a list of PIDs based on the input search string. You can also use pgrep command to search for all the commands by their name and use the kill command to kill those processes in Linux.

The following command will list PIDs of all processes with name apache

$ pgrep apache
6123
6230

Here’s the command to kill all processes by name apache

$ kill -9 `pgrep apache`

The pgrep command will output a list of PIDs with name apache, which are used as input for kill command.

Bonus Read : List all Processes by Name, User, PID

3. Using pidof

Similarly, you can also use pidof command to get the list of PIDs for a process name

$ pidof apache
6123 6230

Once you have retrieved the PIDs of desired processes, you can terminate it using kill command.

$ kill -9 6123 6230

You can pass this list to kill command to kill all processes by name.

$ kill -9 `pidof apache`

Bonus Read : How to Search a File in Linux

4. Using killall

You can also use killall command to kill all processes by name or other attributes. It works like kill command but works on multiple processes at once.

Here is its syntax.

killall [options] process_name

Here is an example to kill all processes containing the string ‘apache’ in its name.

$ killall -9 apache

If you do not have the permission to kill processes, you may need to add sudo keyword before your killall command.

Please note, when you use killall command as-is, it will look for the process containing the specific string. If you want to kill only the process whose name exactly matches the given pattern, then you need to use -e option.

Also, by default, killall command will terminate processes without asking for any confirmation. If you want a confirmation prompt before process termination, use -i option.

Like in the case of kill command, you can also specify the specific signal to send, using -s option.

Conclusion

In this article, we have learnt several important ways to kill processes by name. If a process becomes slow or unresponsive, or your system itself faces these problems, then have no option to terminate the process. In fact, even if it is consuming too much system resources, or there is a software bug that prevents it from closing gracefully after it is done, you need to do the same. You can use any of the pkill, pgrep, pidof and killall commands for this purpose. You just need to be careful about killing multiple processes, while killing them by name. Hopefully, the above article will help you kill all processes by name in Linux.