Atlantic.Net Blog

How to Kill Running Processes in Linux

In Linux, an instance of a running service, application, or script is called a process. When you run any command, program, or application, a process is created for it. Each process has its own process id that is associated with a specific user and group. If any processes consume too many resources or become unresponsive, then you may need to kill them. In this case, you can use the kill command to stop the process without restarting your server.

In this post, we will show you how to kill running processes in Linux.

View Running Processes in Linux

The simplest and easiest way to get a list of all running processes is to use the top command. You can run the top command as shown below to list all running processes:

top

You will get all running processes in the following output:

Tasks: 344 total,   1 running, 297 sleeping,   0 stopped,   0 zombie
%Cpu(s): 22.6 us,  6.0 sy,  0.1 ni, 67.1 id,  3.9 wa,  0.0 hi,  0.4 si,  0.0 st
KiB Mem :  7580260 total,  2103076 free,  3174408 used,  2302776 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  3901388 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                   
 5994 vyom      20   0   44344   4160   3444 R  11.8  0.1   0:00.03 top                                                                       
 2206 vyom      20   0 1046476  89792  60368 S   5.9  1.2   1:15.26 Xorg                                                                      
 2359 vyom      20   0 3471324 216412  97596 S   5.9  2.9   1:58.27 gnome-shell                                                               
 2745 vyom      20   0 1776552 136144  99424 S   5.9  1.8   1:09.93 skypeforlinux                                                             
 2775 vyom      20   0 37.371g 169524 117660 S   5.9  2.2   1:05.09 skypeforlinux                                                             
 5655 root      20   0       0      0      0 I   5.9  0.0   0:00.30 kworker/u8:3-ev                                                           
    1 root      20   0  160356   9464   6632 S   0.0  0.1   0:03.50 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                                                           
    9 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq                                                              
   10 root      20   0       0      0      0 S   0.0  0.0   0:00.40 ksoftirqd/0                                                               
   11 root      20   0       0      0      0 I   0.0  0.0   0:03.58 rcu_sched                                                                 
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 migration/0                                                               
   13 root     -51   0       0      0      0 S   0.0  0.0   0:00.00 idle_inject/0                                                             
   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                                                             

You can press k and enter the process ID to kill the process directly from the top interface.

Also Read

Find Top 10 Running Processes by Memory and CPU Usage

Find the Process Using the ps Command

ps is a command-line utility that will display a complete listing of running processes in the specified format.

The basic syntax of the ps command is shown below:

ps [OPTION]

A brief explanation of each option is shown below:

  • -a – Display running processes of all users.
  • -u – Display detailed information about each of the processes.
  • -x – Display processes that are controlled by the daemon.

For example, run the following command to get a detailed process list of all processes.

ps aux

You will get the following output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.1 160356  9464 ?        Ss   15:51   0:03 /sbin/init splash
root         2  0.0  0.0      0     0 ?        S    15:51   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   15:51   0:00 [rcu_gp]
root         4  0.0  0.0      0     0 ?        I<   15:51   0:00 [rcu_par_gp]
root         6  0.0  0.0      0     0 ?        I<   15:51   0:00 [kworker/0:0H-kb]
root         9  0.0  0.0      0     0 ?        I<   15:51   0:00 [mm_percpu_wq]
root        10  0.0  0.0      0     0 ?        S    15:51   0:00 [ksoftirqd/0]
root        11  0.1  0.0      0     0 ?        I    15:51   0:03 [rcu_sched]
root        12  0.0  0.0      0     0 ?        S    15:51   0:00 [migration/0]
root        13  0.0  0.0      0     0 ?        S    15:51   0:00 [idle_inject/0]
root        14  0.0  0.0      0     0 ?        S    15:51   0:00 [cpuhp/0]
root        15  0.0  0.0      0     0 ?        S    15:51   0:00 [cpuhp/1]
root        16  0.0  0.0      0     0 ?        S    15:51   0:00 [idle_inject/1]
root        17  0.0  0.0      0     0 ?        S    15:51   0:00 [migration/1]
root        18  0.0  0.0      0     0 ?        S    15:51   0:00 [ksoftirqd/1]
root        20  0.0  0.0      0     0 ?        I<   15:51   0:00 [kworker/1:0H-kb]
root        21  0.0  0.0      0     0 ?        S    15:51   0:00 [kdevtmpfs]
root        22  0.0  0.0      0     0 ?        I<   15:51   0:00 [netns]

The above command will show you all the running processes. You can use the grep command with the ps command to get a PID of a specific process.

For example, to get a PID of an Apache process, run the following command:

ps aux | grep apache

You will get the following output:

root      1309  0.0  0.0  73992  4748 ?        Ss   15:52   0:00 /usr/sbin/apache2 -k start
www-data  1310  0.0  0.0 1285068 4620 ?        Sl   15:52   0:00 /usr/sbin/apache2 -k start
www-data  1311  0.0  0.0 1285068 4644 ?        Sl   15:52   0:00 /usr/sbin/apache2 -k start

You can also use the pidof command to get a PID of a specific process.

pidof apache2

You will get the following output:

1311 1310 1309

Kill a Process in Linux

At this point, you know how to get the PID of any process. Now, you can use the kill command to kill a running process.

To kill a single process, run the following command:

kill PID

Or

kill -9 PID

To kill multiple processes, run the following command:

kill -9 PID1 PID2 PID3

You can also kill the processes by their names. You can use the pkill command to kill a process by its name.

To kill the Apache process, run the following command:

pkill apache2

You can also use the killall command to kill a process by its name:

killall mysqld

Also Read

Install Ntopng to Monitor Network Traffic on Ubuntu 20.04

Conclusion

In this post, we explained how to find and kill a process in Linux. You can now kill any unresponsive processes by yourself without restarting your server. Try it on your virtual private server from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year