how to kill process by name in linux

How to Kill Process by Name in Linux

Last updated on April 12th, 2021 at 03:47 am

Linux provides many tools to manage system processes. You can use them to create, clone, and even destroy processes. 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.


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. 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. For example, here’s the command to search and kill process with command /etc/apache/bin/apache

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

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

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.

$ killall -9 apache

Hopefully, the above article will help you kill all processes by name in Linux.