Open In App

How to Kill Processes by Given Partial Names in Linux

Last Updated : 08 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

On a Unix system creates a separate environment for a program when it is executed. Everything the system needs to run the program as if there were no other programs in this environment. In Unix, each command you issue initiates or starts a new process. You initiated a process using the ls command to list the directory contents.
Simply put, a process is a running program instance.  pkill is a utility, preinstalled on most Linux systems, used to terminate processes from the terminal. Processes can be killed using various attributes including partial names. Partial names, snippets, and patterns can surely be used to terminate a process on a Linux machine however it may backfire to kill processes without their unique id. pkill is a powerful command that can terminate/kill processes.

Kill Processes by Given Partial Names in Linux

We can kill processes by giving partial names in Linux by using 3 different methods:

  • Method 1: Kill a Process with the Help of /proc/<pid>/stat.
  • Method 2: Kill a Process with the Help of pgrep
  • Method 3: Kill Multiple Processes Using killall

Let’s go through all the methods one by one:

Syntax of pkill: 

pkill [options] pattern

Using pkill to Kill Processes

To use this command to kill processes using only a partial name we have to add the f flag to the command. When the -f is set the pattern is matched with the full command that started the process.

pkill -f pattern

Setting up a Process to Kill

Let’s first create a new process.

exec -a process_1 sleep 60000 &
exec -a process_1 sleep 60000 &

exec -a process_1 sleep 60000 &

Method 1: Kill a Process with the Help of /proc/<pid>/stat

/proc/<pid>/stat is a unique file defined for each process and it can be accessed with the process id.

Reading the file for our dummy process gives the following output:

Command:

cat /proc/326/stat
cat /proc/326/stat

cat /proc/326/stat

Output:

Here we get the name of the process within parentheses as the second word of the output.

This name can be used with pkill to kill that process.

pkill sleep
pkill sleep

pkill sleep

Now to kill it with only a partial name:

pkill -f pro
pkill -f pro

pkill -f pro

Here we’ve only used “pro” as a pattern which got matched with the “process_1”

This wouldn’t have worked if we hadn’t used the -f flag

Issues with this approach

Again, let’s create a few new processes.

new processes

new processes

Here we have two processes that we want to kill, and we have an important process “imp_process_1” terminating which might cause unforeseen consequences.

Let’s try to kill the two processes.

kill the two processes.

kill the two processes.

We killed the important process. This is why it is important to pay attention while using this command.

Method 2: Kill a Process with the Help of pgrep

Command: 

ps aux | grep process

Here pgrep can be used to go through all processes and match the ones with the given pattern. This way we can easily get the pid of the process and kill it effectively.

Here we don’t want to kill the important process so we will only kill the two processes with their pid

kill 355
kill 355

kill 355

Method 3: Kill Multiple Processes Using killall

We can also kill all processes with the same name using the killall command

First, let’s create a few processes:

Command:

exec -a process_1 sleep 60000 &
exec -a process_2 sleep 60000 &
exec -a process_3 sleep 60000 &
exec -a process_4 sleep 60000 &
exec -a process_5 sleep 60000 &
create a few processes:

create a few processes:

Now to kill them:

killall sleep

After executing of the above command, all the newly created processes are being killed in single run.

killall sleep

killall sleep

Frequently Asked Questions

 What is the difference between kill and pkill commands?

The kill command is use PID to terminate a process, while the pkill command can be used to terminate a process based on partial or full name matches.

For Example:

If we need to kill a process with PID `4321`, we use command as follows

kill 4321

If we need to kill all the processes that are running with `firefox`, we use command as follows

pkill firefox

 How can I kill a process that is not responding?

If a process is not responding we can use a kill command with forceful termination service which is `-9` , if our process PID is `1122` , we use command as follows

kill -9 1122

How can I view a list of all running processes?

One can we the list of all running processes by uising `ps` command. Suppose if we want to list all the running processes with their process IDs (PIDs), we use command as follows

ps -ef


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads