This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

Sometimes, it happens that the processes running on your system start behaving weirdly, and you need to kill the process(es) for the good of your system. Linux provides you with built-in utilities to do that using your command line.

In this tutorial, we will guide you the different ways to check and kill a process. But to do that, you first need to know the Process ID of the process.

Locating The Process

You can locate a process by following different ways

Using top Command

You can run the top command to display the list of running processes on your system.

root@codesposts:~$ top

top - 03:12:13 up 29 min,  1 user,  load average: 0.05, 0.01, 0.03
Tasks: 188 total,   1 running, 144 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.5 us,  0.6 sy,  0.0 ni, 95.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.1 st
KiB Mem :  1935492 total,   134124 free,   807684 used,   993684 buff/cache
KiB Swap:   777300 total,   777300 free,        0 used.   842876 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                 
 1025 ubuntu    20   0 3862720 308244 115440 S  10.6 15.9   0:28.38 gnome-shell                                                                                                                             
  793 ubuntu    20   0  720460 118040  60848 S   3.3  6.1   0:06.81 Xorg                                                                                                                                    
 2034 ubuntu    20   0  805784  39768  28328 S   1.7  2.1   0:01.40 gnome-terminal-                                                                                                                         
   10 root      20   0       0      0      0 I   0.3  0.0   0:00.17 rcu_sched                                                                                                                               
 1711 ubuntu    20   0  220928   8248   7512 S   0.3  0.4   0:00.10 ibus-engine-sim                                                                                                                         
 2382 ubuntu    20   0   51308   4112   3372 R   0.3  0.2   0:00.24 top                                                                                                                                     
    1 root      20   0  225460   9200   6680 S   0.0  0.5   0:03.57 systemd                                                                                                                                 
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd                                                                                                                                
    3 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_gp                                                                                                                                  
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_par_gp                                                                                                                              
    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H-kb                                                                                                                         
    7 root      20   0       0      0      0 I   0.0  0.0   0:00.13 kworker/u8:0-ev                                                                                                                         
    8 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq                                                                                                                            
    9 root      20   0       0      0      0 S   0.0  0.0   0:00.01 ksoftirqd/0                                                                                                                             
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/0                                                                                                                             
   12 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/0                                                                                                                           
   13 root      20   0       0      0      0 I   0.0  0.0   0:00.33 kworker/0:1-mm_                                                                                                                         
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0                                                                                                                                 
   15 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1                                                                                                                                 
   16 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/1                                                                                                                           
   17 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/1                                                                                                                             
   18 root      20   0       0      0      0 S   0.0  0.0   0:00.01 ksoftirqd/1                                                                                                                             
   20 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/1:0H-kb                                                                                                                         
   21 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/2                                                                                                                                 
   22 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/2                                                                                                                           
   23 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/2                                                                                                                             
   24 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/2                                                                                                                             
   25 root      20   0       0      0      0 I   0.0  0.0   0:03.12 kworker/2:0-eve                                                                                                                         
   26 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/2:0H-kb                                                                                                                         
   27 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/3                                                                                                                                 
   28 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/3                                                                                                                           
   29 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/3                                                                                                                             
   30 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/3                                                                                                                             
   32 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/3:0H-kb                                                                                                                         
   33 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs                                                                                                                               
   34 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 netns                                                                                                                                   
   35 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_tasks_kthre                                                                                                                         
   36 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kauditd          

You can see the PID and other information from the output of this command.

Using ps Command

You can also use ps command to display the processes running on your system. Run the command like below:

root@codesposts:~$ ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.4 225460  9200 ?        Ss   02:42   0:03 /sbin/init splash
root       720  0.0  0.5 311336 10908 ?        Ssl  02:42   0:00 /usr/lib/policykit-1/polkitd --no-debug
root       730  0.0  1.0 194392 20140 ?        Ssl  02:42   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
root       743  0.0  0.4 308180  8144 ?        Ssl  02:42   0:00 /usr/sbin/gdm3
root       762  0.0  0.4 419456  9068 ?        Sl   02:42   0:00 gdm-session-worker [pam/gdm-autologin]
root       766  0.0  0.3  25988  6160 ?        S    02:42   0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager/nm-dhcp-helper -pf /run/dhclient-ens3.pid -lf /var/lib/NetworkManager/dhclient-44975ba0-cd
ubuntu     772  0.0  0.4  77008  8340 ?        Ss   02:42   0:00 /lib/systemd/systemd --user
ubuntu     773  0.0  0.1 114072  2668 ?        S    02:42   0:00 (sd-pam)
ubuntu     786  0.0  0.3 288372  7028 ?        SLl  02:42   0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
ubuntu     790  0.0  0.3 212128  5924 tty1     Ssl+ 02:42   0:00 /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu
ubuntu     793  0.3  6.0 720268 117936 tty1    Rl+  02:42   0:08 /usr/lib/xorg/Xorg vt1 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
ubuntu     811  0.0  0.3  51560  5960 ?        Ss   02:42   0:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
ubuntu     814  0.0  0.8 569440 15896 tty1     Sl+  02:42   0:00 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu
ubuntu     933  0.0  0.0  11300   316 ?        Ss   02:42   0:00 /usr/bin/ssh-agent /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu
ubuntu     948  0.0  0.3 291952  6956 ?        Ssl  02:42   0:00 /usr/lib/gvfs/gvfsd
ubuntu     953  0.0  0.4 432020  7900 ?        Sl   02:42   0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
ubuntu     968  0.0  0.4 367828  8752 ?        Ssl  02:42   0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher
ubuntu     976  0.0  0.2  49924  4220 ?        S    02:42   0:00 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
ubuntu     979  0.0  0.3 220776  6804 ?        Sl   02:42   0:00 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session
ubuntu    1025  1.4 15.9 3863552 308896 tty1   Rl+  02:42   0:34 /usr/bin/gnome-shell
root      1032  0.0  0.4 322316  8732 ?        Ssl  02:42   0:00 /usr/lib/upower/upowerd
ubuntu    1053  0.0  0.6 1172140 11776 ?       S<l  02:42   0:00 /usr/bin/pulseaudio --start --log-target=syslog
rtkit     1054  0.0  0.1 183500  3056 ?        SNsl 02:42   0:00 /usr/lib/rtkit/rtkit-daemon
ubuntu    1068  0.0  0.5 377536  9692 tty1     Sl   02:42   0:00 ibus-daemon --xim --panel disable
ubuntu    1070  0.0  0.2 271560  5168 ?        Ssl  02:42   0:00 /usr/libexec/xdg-permission-store
whoopsie  1086  0.0  0.6 392828 12944 ?        Ssl  02:42   0:00 /usr/bin/whoopsie -f
kernoops  1091  0.0  0.0  56936   416 ?        Ss   02:42   0:00 /usr/sbin/kerneloops --test
kernoops  1096  0.0  0.0  56936   424 ?        Ss   02:42   0:00 /usr/sbin/kerneloops
ubuntu    1139  0.0  0.4 296784  8432 tty1     Sl   02:42   0:00 /usr/lib/ibus/ibus-dconf
ubuntu    1142  0.0  1.1 356260 22220 tty1     Sl   02:42   0:00 /usr/lib/ibus/ibus-x11 --kill-daemon
ubuntu    1149  0.0  0.4 294596  8516 ?        Sl   02:42   0:00 /usr/lib/ibus/ibus-portal
ubuntu    1178  0.0  1.1 696516 22016 ?        Sl   02:42   0:00 /usr/lib/gnome-shell/gnome-shell-calendar-server
ubuntu    1184  0.0  1.3 694404 25976 ?        Ssl  02:42   0:00 /usr/lib/evolution/evolution-source-registry
ubuntu    1193  0.0  1.6 778304 31596 ?        Sl   02:42   0:00 /usr/lib/gnome-online-accounts/goa-daemon
ubuntu    1202  0.0  0.5 319548  9736 ?        Sl   02:42   0:00 /usr/lib/gnome-online-accounts/goa-identity-service
root      1210  0.0  0.4 315228  8944 ?        Ssl  02:42   0:00 /usr/lib/x86_64-linux-gnu/boltd
ubuntu    1215  0.0  0.5 320052 10608 ?        Ssl  02:42   0:00 /usr/lib/gvfs/gvfs-udisks2-volume-monitor
ubuntu    1219  0.0  0.3 288656  6608 ?        Ssl  02:42   0:00 /usr/lib/gvfs/gvfs-gphoto2-volume-monitor
ubuntu    1223  0.0  0.3 378812  7416 ?        Ssl  02:42   0:00 /usr/lib/gvfs/gvfs-afc-volume-monitor
ubuntu    1228  0.0  0.2 275868  4964 ?        Ssl  02:42   0:00 /usr/lib/gvfs/gvfs-mtp-volume-monitor

You can also filter your output for a specific process using pipe with grep command.

root@codesposts:~$ ps aux | grep java

ubuntu    2417  0.0  0.0  21532  1116 pts/0    S+   03:25   0:00 grep --color=auto java

This will only display the processes containing the keyword “java” in them.

Using pidof/pgrep Command

You can find the PID of a specific process by running the command like below:

root@codesposts:~$ pidof bash
2044

OR 

root@codesposts:~$ pgrep bash
2044

Note the PID and Name of the process you want to kill.

Killing The Process

You can kill a process by the following ways

Killing A Process By PID

You can kill the process by using PID by executing kill command.

There are different signals available to be used with the kill command. you can display them by running the following command:

root@codesposts:~$ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

Most commenly used signals are 1,9 and 15. If you want to hangup the process, you can use 1. If you want to kill a process without saving it’s content, you can use the signal 9. If you want to kill the process and saving the contents before killing, you can use the signal 15.

root@codesposts:~$ kill -9 2044 

This command will kill the process with PID 2044 without saving the contents.

Killing A Process By Name

You can kill a process using it’s name by executing the killall command.

root@codespsots:~$ killall httpd

Killing A Process Using pkill Command

You can also use the pkill command to kill a process if you know the name of the process.

root@codesposts:~$ pkill httpd

You can also use different options with the pkill command.

root@codesposts:~$ pkill --help

Usage:
 pkill [options] <pattern>

Options:
 -<sig>, --signal <sig>    signal to send (either number or name)
 -e, --echo                display what is killed
 -c, --count               count of matching processes
 -f, --full                use full process name to match
 -g, --pgroup <PGID,...>   match listed process group IDs
 -G, --group <GID,...>     match real group IDs
 -i, --ignore-case         match case insensitively
 -n, --newest              select most recently started
 -o, --oldest              select least recently started
 -P, --parent <PPID,...>   match only child processes of the given parent
 -s, --session <SID,...>   match session IDs
 -t, --terminal <tty,...>  match by controlling terminal
 -u, --euid <ID,...>       match by effective IDs
 -U, --uid <ID,...>        match by real IDs
 -x, --exact               match exactly with the command name
 -F, --pidfile <file>      read PIDs from file
 -L, --logpidfile          fail if PID file is not locked
 --ns <PID>                match the processes that belong to the same
                           namespace as <pid>
 --nslist <ns,...>         list which namespaces will be considered for
                           the --ns option.
                           Available namespaces: ipc, mnt, net, pid, user, uts
 -h, --help     display this help and exit
 -V, --version  output version information and exit

Killing A Process Using top Command

You can also kill a process using the top command. Just run the top command in your terminal and note the PID of the process you want to kill, then press k while keeping the top command running and it will ask you for the PID of the process you want to kill.

root@codesposts:~$ top

top - 04:23:14 up  1:40,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 195 total,   1 running, 152 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.6 us,  0.1 sy,  0.0 ni, 99.0 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1935492 total,   195672 free,   820008 used,   919812 buff/cache
KiB Swap:   777300 total,   776520 free,      780 used.   831924 avail Mem 
PID to signal/kill [default pid = 1025] 
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                 
 1025 ubuntu    20   0 3862528 309512 115560 S  41.2 16.0   1:13.90 gnome-shell                                                                                                                             
  793 ubuntu    20   0  720268 117936  60744 S  17.6  6.1   0:19.74 Xorg                                                                                                                                    
 3053 root      20   0   51348   4128   3424 R   5.9  0.2   0:00.02 top                                                                                                                                     
    1 root      20   0  225460   9200   6680 S   0.0  0.5   0:03.69 systemd                                                                                                                                 
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd                                                                                                                                
    3 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_gp                                                                                                                                  
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_par_gp                                                                                                                              
    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H-kb                                                                                                                         
    8 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq                                                                                                                            
    9 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/0                                                                                                                             
   10 root      20   0       0      0      0 I   0.0  0.0   0:00.35 rcu_sched                                                                                                                               
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 migration/0                                                                                                                             
   12 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/0                                                                                                                           
   13 root      20   0       0      0      0 I   0.0  0.0   0:00.48 kworker/0:1-eve                                                                                                                         
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0                                                                                                                                 
   15 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1                                                                                                                                 
   16 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/1                                                                                                                           
   17 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/1                                                                                                                             
   18 root      20   0       0      0      0 S   0.0  0.0   0:00.01 ksoftirqd/1                                                                                                                             
   20 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/1:0H-kb                                                                                                                         
   21 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/2                                                                                                                                 
   22 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/2                                                                                                                           
   23 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/2                                                                                                                             
   24 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/2                                                                                                                             
   25 root      20   0       0      0      0 I   0.0  0.0   0:03.29 kworker/2:0-eve                                                                                                                         
   26 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/2:0H-kb                                                                                                                         
   27 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/3                                                                                                                                 
   28 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/3                                                                                                                           
   29 root      rt   0       0      0      0 S   0.0  0.0   0:00.02 migration/3                                                                                                                             
   30 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/3                                                                                                                             
   32 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/3:0H-kb                                                                                                                         
   33 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs                                                                                                                               
   34 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 netns                                                                                                                                   
   35 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_tasks_kthre                                                                                                                         
   36 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kauditd                                                                                                                                 
   37 root      20   0       0      0      0 S   0.0  0.0   0:00.00 khungtaskd                                                                                                                              
   38 root      20   0       0      0      0 S   0.0  0.0   0:00.00 oom_reaper                                                                                                                              
   39 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 writeback                                                                                                                               
   40 root      20   0       0      0      0 S   0.0  0.0   0:00.08 kcompactd0                                                                                                                              
   41 root      25   5       0      0      0 S   0.0  0.0   0:00.00 ksmd                                                                                                                                    
   42 root      39  19       0      0      0 S   0.0  0.0   0:00.00 khugepaged                                                                                                                              
   43 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 crypto