How to Kill a Process from Linux Terminal

Channel: Linux
Abstract: killing the process by its name will terminate all running processes of a program. Kill a process by PID To see a list of all the running processes an

Everything running in the Linux operating system is considered as a process. When a user executes a program or the Linux OS runs a system service itself, a new process will be created.

Usually, the processes will be terminated when they finished their tasks, or the user forces them to terminate by using mouse to click on the "Close" or pressing "Ctrl +C" on keyboard.

Nevertheless, we may encounter unexpected behavior when a process hang up and occupy almost all system resources like CPU or RAM. In order to free up resources, we have to manually terminate or "kill" that process.

How to use the Command Line or Term...

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

How to use the Command Line or Terminal for Absolute Beginners

In this tutorial, we will go through how to kill a process in Linux.

The mechanism of kill command

The name of the command 'kill' tells us it is used to immediately stop a process on Linux system. 'kill' command will try to end the processes you want to kill as gracefully as possible.

'Kill' command sends a specified signal to the process. The signal could be SIGTERM (Terminate) or SIGKILL (Kill Signal). The SIGTERM signal has a numeric value of 15, the SIGKILL signal has a numeric value of 9.

By default, sending a SIGTERM signal is safest way to kill the process. It instructs the process to terminate and gives the programs or services time to wrap things up before be shutdown such as writing to log files or closing opened connections that were created by the process.

On the other hand, the SIGKILL signal is an unsafe way for killing a process because it ends process without saving anything.

The SIGKILL signal is really effective when a user want to kill a process running stubbornly.

Locate the running processes

Every process in the Linux system is an instance of a program. Every time, a program is started by the operating system, it is assigned a unique PID - which stands for Process Identification number.

As a result, there could not be two processes have the same PID in a Linux system because the operating system uses the Process ID to track every process.

In order to kill a process, we should first specify the details of the process by running 'ps command', 'top', 'pidof' or 'pgrep'.

Later in this tutorial, I will explain more about the way to use the above commands. Remember that when you kill the process by its PID, only a specific process is killed. Whereas, killing the process by its name will terminate all running processes of a program.

Kill a process by PID

To see a list of all the running processes and corresponding pid, use 'ps command':

$ ps aux

USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root          1  0.0  0.3 159904  7152 ?        Ss   Thg 611   0:10 /sbin/init splash
root          2  0.0  0.0      0     0 ?        S    Thg 611   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        I<   Thg 611   0:00 [rcu_gp]
root          4  0.0  0.0      0     0 ?        I<   Thg 611   0:00 [rcu_par_gp]
root          6  0.0  0.0      0     0 ?        I<   Thg 611   0:00 [kworker/0:0H-kb]
root          9  0.0  0.0      0     0 ?        I<   Thg 611   0:00 [mm_percpu_wq]
root         10  0.0  0.0      0     0 ?        S    Thg 611   0:02 [ksoftirqd/0]
root         11  0.0  0.0      0     0 ?        I    Thg 611   2:48 [rcu_sched]
root         12  0.0  0.0      0     0 ?        S    Thg 611   0:00 [migration/0]
root         13  0.0  0.0      0     0 ?        S    Thg 611   0:00 [idle_inject/0]
root         14  0.0  0.0      0     0 ?        S    Thg 611   0:00 [cpuhp/0]
root         15  0.0  0.0      0     0 ?        S    Thg 611   0:00 [cpuhp/1]
root         16  0.0  0.0      0     0 ?        S    Thg 611   0:00 [idle_inject/1]
root         17  0.0  0.0      0     0 ?        S    Thg 611   0:01 [migration/1]
root         18  0.0  0.0      0     0 ?        S    Thg 611   0:00 [ksoftirqd/1]
root         20  0.0  0.0      0     0 ?        I<   Thg 611   0:00 [kworker/1:0H-kb]
root         21  0.0  0.0      0     0 ?        S    Thg 611   0:00 [kdevtmpfs]
root         22  0.0  0.0      0     0 ?        I<   Thg 611   0:00 [netns]
root         23  0.0  0.0      0     0 ?        S    Thg 611   0:00 [rcu_tasks_kthre]
root         24  0.0  0.0      0     0 ?        S    Thg 611   0:00 [kauditd]
...
ps aux command output

Once the process id is determined, you can terminate the process by using 'kill' command following with its pid. For example:

$ sudo kill <process id>

The above command sends a SIGTERM signal to the process and terminate it. You can verify whether the process has close successfully by running the command:

$ ps aux | grep <process id>

Some important things to be noted:

  • A user can kill all of its processes
  • A user can not kill processes of other users
  • A user can not kill a process is running by the system
  • Root can kill processes of the system and processes of any user

You can run kill command with 'sudo' for killing a process as a 'root'.

Another way to see a list of the running processes is using 'top' command:

$ top

top - 16:50:31 up 3 days, 53 min,  1 user,  load average: 0,00, 0,00, 0,00
Tasks: 257 total,   1 running, 186 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0,2 us,  0,3 sy,  0,0 ni, 99,5 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
KiB Mem :  2006008 total,  1209480 free,   443172 used,   353356 buff/cache
KiB Swap:   969960 total,   922100 free,    47860 used.  1403360 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                 
    11 root      20   0       0      0      0 I   0,3  0,0   2:48.96 rcu_sched                                                                               
     1 root      20   0  159904   7152   5400 S   0,0  0,4   0:10.25 systemd                                                                                 
     2 root      20   0       0      0      0 S   0,0  0,0   0:00.03 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:02.56 ksoftirqd/0                                                                             
    12 root      rt   0       0      0      0 S   0,0  0,0   0:00.89 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                                                                           
    17 root      rt   0       0      0      0 S   0,0  0,0   0:01.09 migration/1                                                                             
    18 root      20   0       0      0      0 S   0,0  0,0   0:00.94 ksoftirqd/1                                                                             
    20 root       0 -20       0      0      0 I   0,0  0,0   0:00.00 kworker/1:0H-kb   
top command show pid

As you can see in the output of top command, the PID of the process is in the left column and the name of the corresponding program is on the right.

In addition, if you want to get the PID of a process which you know its name, run the below command line:

$ pidof <program-name>

For example, in order to get the PID of a process has name of 'netns':

$ pidof netns
22

Even more, if you don't know the exact name of the process, you can run the command 'pgrep'. For example, in order to search for all of the processes contain "netn", run the command:

$ pgrep netn
22

Sometimes, you may encounter a process that is hanging-up and refusing to terminate, you can kill that process by sending the SIGKILL signal with option -9

$ sudo kill -9 <process id>
Kill a process by name

This section will explain the way to kill a process using process name. There are many commands which we can use to kill a process by its name such as: 'killall' and 'pkill'.

Using killall command to kill a process if you know the exact process's name. Run the command line:

$ killall <process name>

For example, killing a process named "nginx", run the command line:

$ killall nginx

If you want to forcibly kill a process, sending the SIGKILL signal to this process:

$ killall -9 nginx

There are some other scenarios of using 'killall' command. For instance:

Killing a process which has been running for more than 10 mins, run the command line:

$ killall -o 10m <process name> 

Killing a process which has been running for less than 10 mins, run:

$ killall -y 10m <process name>

Sometimes, you don't know the exact name of a program, you only know a part of the name, you can use 'pkill' command to kill that process. Here is an example:

$ pkill ssh

If there are some other processes named 'ssh-server' or 'ssh-agent', the above command 'pkill' will also kill both of them.

Conclusion

In this tutorial, we learned many ways to kill the process in Linux. We went through the detail of how to use 'kill' command and 'pkill' command to terminate a process by PID or by name.

With the experience of using the 'kill' command and from the above hands-on practices, you could supervise over the processes running on operating system.

Thanks for reading and please leave your suggestion in the below comment section.

Ref From: linoxide
Channels:

Related articles