Skip to content

How to kill a process in Linux?

Learn how to kill a running process in Linux using Kill, Pkill and Killall commands. These commands terminates processes abruptly and make sure the system continues to run.

In Linux, usually a process terminates on its own one its job is done. But sometimes a process can take up too much time and CPU and it would still keep on running. In such situations, we need to kill the process.

To kill a process, we need its pid (process id). First we will take a look at different ways to finding pid.

Locating a process using Linux commands

In Linux, every process has a pid. The kernel identifies each process using a process ID (PID), a every instance of process must have a unique PID from other processes which is assigned when the process is invoked, to avoid any execution errors.

ps command

To view the list of all running processes using the following command.

ps aux

Its output would list all the running processes with the user, pid, percentage of CPU utilization, percentage of memory consumed etc. But this command would show the processes run by the root user too.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 167384  8820 ?        Ss   14:59   0:08 /sbin/init persistent splash
root         2  0.0  0.0      0     0 ?        S    14:59   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   14:59   0:00 [rcu_gp]
root         4  0.0  0.0      0     0 ?        I<   14:59   0:00 [rcu_par_gp]
root         6  0.0  0.0      0     0 ?        I<   14:59   0:00 [kworker/0:0H-kblockd]
root         8  0.0  0.0      0     0 ?        I<   14:59   0:00 [mm_percpu_wq]
root         9  0.0  0.0      0     0 ?        S    14:59   0:02 [ksoftirqd/0]
root        10  0.3  0.0      0     0 ?        I    14:59   0:43 [rcu_sched]
root        11  0.0  0.0      0     0 ?        S    14:59   0:00 [migration/0]
root        12  0.0  0.0      0     0 ?        S    14:59   0:00 [idle_inject/0]
root        14  0.0  0.0      0     0 ?        S    14:59   0:00 [cpuhp/0]
root        15  0.0  0.0      0     0 ?        S    14:59   0:00 [cpuhp/1]
root        16  0.0  0.0      0     0 ?        S    14:59   0:00 [idle_inject/1]
root        17  0.0  0.0      0     0 ?        S    14:59   0:00 [migration/1]
root        18  0.0  0.0      0     0 ?        S    14:59   0:00 [ksoftirqd/1]
root        20  0.0  0.0      0     0 ?        I<   14:59   0:00 [kworker/1:0H-kblockd]
root        21  0.0  0.0      0     0 ?        S    14:59   0:00 [cpuhp/2]
root        22  0.0  0.0      0     0 ?        S    14:59   0:00 [idle_inject/2]

We can filter this out by using grep.

ps aux | grep william

This command would list down all the processes started by the user william.

william    1561  0.0  0.0  16800  6748 ?        Ss   15:00   0:00 /lib/systemd/systemd --user
william    1562  0.0  0.0  21264  2480 ?        S    15:00   0:00 (sd-pam)
william    1578  0.0  0.0 542848  7376 ?        Sl   15:00   0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
william    1582  0.0  0.0 173872  5472 tty2     Ssl+ 15:00   0:00 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu /usr/bin/gnome-session --session=ubuntu
william    1584  1.3  0.5 781140 47000 tty2     Sl+  15:00   2:43 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
william    1596  0.0  0.0   8144  4504 ?        Ss   15:00   0:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
william    1599  0.0  0.1 583220 12020 tty2     Sl+  15:00   0:00 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu
william    1705  0.0  0.0   5924    40 ?        Ss   15:00   0:00 /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu /usr/bin/gnome-session --session=ubuntu
william    1711  0.0  0.0 308716  5776 ?        Ssl  15:00   0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher
william    1716  0.0  0.0   7264  3592 ?        S    15:00   0:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
william    1718  0.0  0.0 168188  5576 ?        Sl   15:00   0:00 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session
william    1739  2.1  2.3 3366092 189992 tty2   Rl+  15:00   4:16 /usr/bin/gnome-shell
william    1749  0.0  0.0 247448  6300 ?        Ssl  15:00   0:00 /usr/lib/gvfs/gvfsd
william    1754  0.0  0.0 377580  4964 ?        Sl   15:00   0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
william    1766  0.1  0.1 319296  8196 tty2     Sl   15:00   0:17 ibus-daemon --xim --panel disable
william    1767  0.0  0.0 243300  3972 ?        Ssl  15:00   0:00 /usr/libexec/xdg-permission-store

We can also filter it using the name of the program.

ps aux | grep httpd

This command would filter out the Apache processes

root     15720  0.0  0.0 224064  2956 ?        Ss   01:43   0:02 /usr/sbin/httpd -DFOREGROUND
root     16209  0.0  0.0 241228  2572 pts/0    S+   07:29   0:00 sudo vi httpd.conf
root     16230  0.0  0.0 126504  1268 pts/0    S+   07:30   0:00 vi httpd.conf
schandr+ 19168  0.0  0.0 112716   960 pts/1    S+   07:53   0:00 grep --color=auto httpd
apache   25863  0.0  0.0 224196  2644 ?        S    03:27   0:00 /usr/sbin/httpd -DFOREGROUND
apache   25864  0.0  0.0 224196  2536 ?        S    03:27   0:00 /usr/sbin/httpd -DFOREGROUND
apache   25865  0.0  0.0 224196  2640 ?        S    03:27   0:00 /usr/sbin/httpd -DFOREGROUND
apache   25866  0.0  0.0 224196  2620 ?        S    03:27   0:00 /usr/sbin/httpd -DFOREGROUND
apache   25867  0.0  0.0 224196  2508 ?        S    03:27   0:00 /usr/sbin/httpd -DFOREGROUND
apache   26512  0.0  0.0 224196  2536 ?        S    03:32   0:00 /usr/sbin/httpd -DFOREGROUND
apache   26579  0.0  0.0 224196  2564 ?        S    03:33   0:00 /usr/sbin/httpd -DFOREGROUND
apache   26650  0.0  0.0 224196  2496 ?        S    03:34   0:00 /usr/sbin/httpd -DFOREGROUND

pidof and pgrep commands

The pidof command lists down all the pids using the process name.

>pidof httpd
26650 26579 26512 25867 25866 25865 25864 25863 15720


Similarly, pgrep command lists down all the process ids using its name.

>pgrep httpd
15720
25863
25864
25865
25866
25867
26512
26579
26650



top command

top command gives you an interface where you can view more information of all processes which can be navigated using up and down keys.

top - 20:48:29 up  5:49,  1 user,  load average: 3.04, 2.16, 1.56
Tasks: 387 total,   1 running, 386 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.5 us,  1.6 sy,  0.0 ni, 92.7 id,  0.1 wa,  0.0 hi,  0.1 si,  0.0 st
MiB Mem :   7837.3 total,    392.2 free,   2887.8 used,   4557.3 buff/cache
MiB Swap:   2048.0 total,   1822.5 free,    225.5 used.   3631.3 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                 
 1739 william    20   0 3378452 204788  61564 S  11.6   2.6   5:27.05 gnome-shell                                                                                                                             
 1584 william    20   0  786208  47408  34292 S   5.3   0.6   3:06.74 Xorg                                                                                                                                    
 7165 william    20   0 1099620 349188 125504 S   2.6   4.4  12:56.24 chrome                                                                                                                                  
 7632 william    20   0  987264  45048  31456 S   2.3   0.6   0:07.97 gnome-terminal-                                                                                                                         
  912 root      20   0  232908   6028   5324 D   1.0   0.1   3:43.04 iio-sensor-prox                                                                                                                         
 7214 william    20   0  606148 129064  83976 S   1.0   1.6   1:17.45 chrome                                                                                                                                  
 2151 william    20   0  389088  83624  42320 S   0.7   1.0   1:08.93 chrome                                                                                                                                  
 8025 william    20   0  573648 113188  75428 S   0.7   1.4   0:35.61 chrome                                                                                                                                  
 1009 debian-+  20   0  185720   7340   6400 S   0.3   0.1   0:14.14 transmission-da                                                                                                                         
 2092 william    20   0 1520300 554968 361468 S   0.3   6.9   4:57.96 chrome                                                                                                                                  
 5996 william    20   0  893088 261808  81012 S   0.3   3.3   2:03.59 chrome                                                                                                                                  
 7413 william    20   0  705892 215500  82012 S   0.3   2.7   1:32.96 chrome                                                                                                                                  
 7458 william    20   0  590140 132180  78624 S   0.3   1.6   0:49.35 chrome                                                                                                                                  
 8045 william    20   0  575820 101520  71952 S   0.3   1.3   0:37.83 chrome                                                                                                                                  
 8254 william    20   0  850964 188708  94596 S   0.3   2.4   1:22.29 chrome                                                                                                                                  
 8399 william    20   0  547392  87096  66932 S   0.3   1.1   0:14.28 chrome                                                                                                                                  
11625 root      20   0       0      0      0 D   0.3   0.0   0:00.16 kworker/0:2+kacpid                                                                                                                      
11745 root      20   0       0      0      0 I   0.3   0.0   0:00.15 kworker/3:0-events                                                                                                                      
11882 root      20   0       0      0      0 I   0.3   0.0   0:00.23 kworker/u16:69-iwlwifi                                                                                                                  
12216 william    20   0   20660   3956   3208 R   0.3   0.0   0:00.18 top          


Killing processes

When you kill a process, Linux sends a terminating signal to the process. By default, SIGTERM signal will be sent, which allows the program to interpret this signal and run some code to help the process exit gracefully. To kill a process forcibly, we can use SIGKILL signal. SIGTERM is referred as number 15 internally and SIGKILL as 9.

To kill processes, we can use various commands such as kill, pkill and killall.

kill command

To kill a process using kill command, use the following syntax

kill [signal] pid1 pid2 pidN

To send SIGKILL signal

kill -9 pid1 pid2 pidN

You can also pass SIGKILL instead of the number 9.

kill -SIGKILL 5467 4535

killall command

killall command kills a process and all its child process. This command accepts process name. Syntax would be..

kill [signal] <process_name>

Example:

kill -9 httpd

pkill command

pkill command kills a process using its name.

pkill <process_name>

Always be sure you are passing the correct process name. Sending wrong process name would screw things up.

pkill httpd

or..

pkill -9 httpd
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.