To explain the meaning of a ‘Process’ in the simplest words is that it is a running instance of any application or program on your system. You may be running multiple applications simultaneously such as browsing, listening to music working on your terminal, etc. There are many background processes associated with these applications that are run by the user.

Every application or a program that runs on your system creates multiple processes associated with your single application. Sometimes this may be a problem and getting rid of these processes is the only option you have.

‘Killing’ a process is one useful option Linux provides you to stop the ongoing processes, be it a foreground process or a background process. In this article, we will review the commands like kill, pkill and killall to force quit any process on a system.

Why kill a Process?

Understanding the concept of killing a process is important before moving ahead in this tutorial. Killing may seem a very brutal way to express the concept, but what it figuratively means is to forcefully abort a process.

Now, why abort or quit an ongoing process? When multiple processes are running in the background, all or few of them may malfunction and can cause your system to misbehave. This delays your ongoing tasks as the malfunctioning process may freeze your system for a while.

Sometimes, quitting all the misbehaving processes seems to be the only option to restore normalcy on your system. Linux allows you to kill a process using the pid or the process name.


Using the pgrep command

Most of the Linux users are familiar with the grep command. The pgrep command can be used on similar lines of the grep.

pgrep command when used, displays the pid of the running process as specified in the command. This command will prove very helpful while using the pkill command.

General Syntax:

pgrep [options] [pattern]

Important options available with the pgrep command

OptionDescription
-ulist process id owned by a specific user
-ccount number of matching processes
-Ilist only process names
-alist full path of the process name

Let us demonstrate the use of pgrep command using an example.

pgrep -u gaurav gnome

Here, we wish to see the pids of the process gnome that is owned by the user ‘gaurav’. Option -u allows you to list the pids of the processes owned by a specific user. In this case, user gaurav.

Output:

gaurav@ubuntu:~$ pgrep -u gaurav gnome
1752
1755
1909
1922
2021
2576
4279
gaurav@ubuntu:~$

As we move ahead with this tutorial, pgrep command will help us in confirming if the process has been killed or is still running.

Let us now move to the pkill command and its execution.


Using pkill command

You can use the pkill command in Linux to kill the process using the process name. Even if you do not know the pid of some process, even then you can kill that particular process using the pkill command.

The processes can be specified with their complete name or the partial name while using the pkill command. Even if you enter the partial name of the process, the pkill command will match all the running processes with the matching name that you have entered in the command.

Syntax:

pkill [options][process_name_pattern] 

Example:

Let us display the processes currently running using the top command. You can also use the ps command to list the processes.

top
top - 14:24:02 up  3:12,  1 user,  load average: 0.29, 0.48, 0.58
Tasks: 221 total,   1 running, 172 sleeping,   0 stopped,   1 zombie
%Cpu(s):  5.6 us,  1.0 sy,  0.0 ni, 92.9 id,  0.4 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem :  3928240 total,   610456 free,  2233152 used,  1084632 buff/cache
KiB Swap:  4083708 total,  3378884 free,   704824 used.  1187268 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                           
 4077 gaurav   20   0 3312128 673480 118360 S  19.6 17.1  15:13.23 Web Content                                                                       
 3712 gaurav   20   0 3953008 453544 116476 S   4.0 11.5   9:28.39 MainThread                                                                        
 2010 gaurav   20   0 4084232 111096  45024 S   1.7  2.8   3:14.85 gnome-shell                                                                       
 1197 root      20   0 1039612  33704  22988 S   1.0  0.9   3:04.42 Xorg                                                                              
 1426 couchdb   20   0 3772396  16908   2520 S   0.7  0.4   1:50.83 beam.smp                                                                          
 3288 gaurav   20   0  722480  25048  18272 S   0.7  0.6   0:06.84 gnome-terminal-                                                                   
 3915 gaurav   20   0 2804900 231524 111228 S   0.7  5.9   0:54.42 Web Content                                                                       
 4146 gaurav   20   0 3017924 245304 120604 S   0.7  6.2   2:01.21 Web Content                                                                       
 4417 gaurav   20   0 2964208 234396 119160 S   0.7  6.0   0:59.90 Web Content                                                                       
 4860 gaurav   20   0 3066800 372920 132544 S   0.7  9.5   0:48.20 Web Content                                                                       
16007 gaurav   20   0   41944   3780   3116 R   0.7  0.1   0:00.28 top            

Using top command will display multiple processes on your terminal. Let us try to display the process with a particular name. We will use the grep command to display a process whose name matches the string ‘mongo’.

top | grep -i mongo

Note: Here, I have used the -i option to make the search case-insensitive.

The output of this command will display the processes matching to the name ‘mongo’

 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.22 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   1.0  0.1   1:03.25 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.27 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.29 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.31 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.33 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   1.0  0.1   1:03.36 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.38 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.40 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   1.0  0.1   1:03.43 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.45 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   1.0  0.1   1:03.48 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.3  0.1   1:03.49 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   1.0  0.1   1:03.52 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   0.7  0.1   1:03.54 mongod      
 1158 mongodb   20   0  288564   4848   1320 S   1.0  0.1   1:03.57 mongod

Now, we will use the pkill command to kill the process named ‘mongo’.

pkill mongo

This command will now kill the process mongo. We can confirm if the process has been forced to quit by using the pgrep command which displays the pid of the running process according to the criteria specified by the user.

gaurav@ubuntu:~$ pgrep mongo
gaurav@ubuntu:~$

This command will return no value. This confirms that the process ‘mongo’ is now killed using the pkill command.


Options frequently used with pkill command

While using the pkill command we will be needing the mentioned options for proper and effortless use of the pkill command.

OptionsDescription
-fmatch against full arguments including spaces, quotes, special characters
-uto inform the pkill process to match the process being run by the specified user
-1reloads the process
-9kills the process
-15gracefully aborts a proces

Let us see one more example of pkill command using the -f option.

There are two commands currently executing on the terminal as shown below.

ping bbc.com

ping youtube.com

Both the processes are initiated by the ping command. Now, suppose we wish to terminate only one process “ping youtube.com” then we have to use the -f option with the pkill command which kills a process with a specific name including the spaces and quotes from the process name.

Command:

gaurav@ubuntu:~$ pkill -f "ping youtube.com"
gaurav@ubuntu:~$ 

Result:

gaurav@ubuntu:~$ ping youtube.com
PING youtube.com (142.250.67.206) 56(84) bytes of data.
64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=1 ttl=117 time=30.9 ms
64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=2 ttl=117 time=121 ms
64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=206 ttl=117 time=86.5 ms
64 bytes from bom12s08-in-f14.1e100.net (142.250.67.206): icmp_seq=207 ttl=117 time=105 ms
Terminated
gaurav@ubuntu:~$

Here, the “ping youtube.com” process is now killed and the “ping bbc.com” is still running on the terminal.

In case, if we had used the pkill ping command, it would have killed both the ping processes, which is undesirable.


Signals used with pkill command

pkill forces a process to quit by sending a specific signal to that process. There are three possible signals which pkill command could send to the process depending upon the command which the user gives.

Following is the list of the signals available.

Signal Specification
1(HUP)reloads the process specified
9 (KILL)kills the process specified
15 (TERM)gently stops or aborts the process specified

For this tutorial, we will be relying heavily on the KILL signal. Let us go through some examples to understand it better.

Using the pgrep command to get the pid matching against the name apache.

gaurav@ubuntu:~$ pgrep apache
1218
10402
10403
gaurav@ubuntu:~$
pkill -KIll apache

OR you can also use the command with numbers (eg. 1, 9, 15)

pkill -9 apache

Both the commands shown above will kill the process apache. Confirming with the pgrep command again.

gaurav@ubuntu:~$ pgrep apache
gaurav@ubuntu:~$

As the pgrep command returns no output, proves that the process apache has been killed.


Conclusion

In this tutorial, we learnt about the pkill command and how it is used to kill the process using the process name directly. We also learnt about the pgrep command which is used to fetch the process id of the process run by any specific user. The pgrep command allows us to cross-check if the process is killed.