How to Kill a Stuck Process using KILL Command in Linux

As a system administrator, you must have faced the problems with Linux processes. Either the process is hanged or completely unresponsive. Many times restarting the process also doesn’t work. In this situation, the one and only option is to terminate that particular process. Here the kill command comes into picture. In this tutorial, you will learn how to kill a stuck process using KILL Command in Linux.

Prerequisite

  • User with the sudo user or root user privileges.
  • Linux Operating System

Basic Syntax

There are other commands as well that can do the same operation but kill command is popular and most widely used. Let’s have a look on the syntax of kill command.

$  sudo kill -signal <pid>

Here, pid termed as process id. 

Kill command has a lot of signals available to manage the running or hanged processes. You can list all the kill signals available in linux using following command.

$  sudo kill -l

kill command linux

Although there are 64 signals that can be used with kill command but most of them are not used in your entire system administration career.  Let’s have a look on mostly used signals.

Stop or Kill a Process

Stopping or killing a process most often happen. To stop a process, you must know the process id which can be obtained by using the ps command.

$  ps -ef | grep apache2

#output

root             16173       1     0  03:20   ?        00:00:00  /usr/sbin/apache2 -k start
www-data  16176 16173  0  03:20   ?         00:00:00  /usr/sbin/apache2 -k start
www-data  16177 16173  0  03:20   ?         00:00:00  /usr/sbin/apache2 -k start
Ravi            17150 17142  0  03:22 pts/0   00:00:00  grep --color=auto  apache2

You can also use other commands to get the process id. Let’s use following command.

$  pidof apache2

#output

16177 16176 16173

Once you get the process id, you can use it to terminate the process. You can use the kill command the following three ways:

$  sudo kill -9 pid

$  sudo kill -SIGKILL pid

$  sudo kill -KILL pid

You can either use the signal number, signal name or signal name without SIG.

kill command linux

You must have root user level privileges to terminate any process.

Reload a Process

You can also reload the process using kill command. You must have process id handy before that. You can use the following command to reload the process.

$  sudo kill -1 pid

$ sudo kill -SIGHUP pid

$ sudo kill -HUP pid

You can use either of the three commands to reload any process.

Gracefully Stop a Process

If you wan to gracefully stop a process, you can use following commands.

$ sudo kill -15 pid

$ sudo kill -SIGTERM pid

$ sudo kill -TERM pid

Here also you can use either of the three command as you wish.

Conclusion

Hence you learn how to kill a stuck process using KILL command in Linux. I hope you understand it in better way. Stay tuned for more linux tutorials.

Read Also : Difference between Soft Links and Hard Links in Linux

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

Leave a Comment