How to kill the process which is using specific port in Linux
Contents
Find the Process Id of the process in Linux
Linux kernel assigns the unique identifier for each process that is called as Process Id (PID).The first process that runs after booting is called the init process and has the pid 1.The default maximum value of the pid on a linux is 32678.
lsof (List of Open files) command to get the PID
lsof is a command line utility for all Unix and Linux like operating systems to check “list of open files” .The name “lsof” is itself derived from this functionality. lsof command is mainly used to retrieve information about files that are opened by various processes.
Syntax of lsof -i command:
1 |
lsof -i:<port_number> |
The lsof command with option ‘-i’ shows the list of all network connections ‘LISTENING & ESTABLISHED’. Using this command, we can find the process Id that is using the particular port in Linux.
Stop the Process using the Kill command
We can stop the process by mentioning the process id in the kill command. kill is used to send signals to a process, most often to stop processes. This command supports more than 60 signals.However, most users only need to use signal 9 or 15. To get a full list of signals, type:
1 |
kill -l |
Syntax of Kill command to stop the process
1 |
kill -9 <PID> |
Here PID denotes the Process Id of the process. kill -9 means “sends the SIGKILL signal to the process which is mapped with given PID”
Example : Kill the process using lsof and kill command
Lets demonstrate on how to kill the process in Linux
Step 1 : Run the lsof command as below to find the process id which is running on specific port. Here we have mentioned the port number as 18080.
1 |
lsof -i:18080 |
It returns the PID (process id) of the process that is using the port number 18080. Along with that, it showed the user of the process, FD (File descriptor), Type of the process, Device and so on.
1 2 |
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 18928 revisit_batch 19u IPv6 2839928 0t0 TCP *:18080 (LISTEN) |
Step 2 : Run the kill -9 command with PID to terminate the process
1 |
kill -9 18928 |
It sends the SIGKILL signal to the process which has the PID as 18928. It terminated the process and All of the resources that were in use by the process are released.