[linux] How to kill a process running on particular port in Linux?

I tried to close the tomcat using ./shutdown.sh from tomcat /bin directory. But found that the server was not closed properly. And thus I was unable to restart
My tomcat is running on port 8080.

I want to kill the tomcat process running on 8080. I first want to have the list of processes running on a specific port (8080) in order to select which process to kill.

This question is related to linux unix port kill-process

The answer is


  1. lsof -i tcp:8000 This command lists the information about process running in port 8000

  2. kill -9 [PID] This command kills the process


This is the solution for Windows:

C:\Users\Niroshan>netstat -ano|findstr "PID :8080"

Proto Local Address Foreign Address State PID
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 18264

taskkill /pid 18264 /f

to build on what @veer7 said:

if you want to know what was on the port, do this before you kill it.

$ sudo netstat -plten |grep java
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      1000       906726      25296/java      
tcp6       0      0 :::8009                 :::*                    LISTEN      1000       907503      25296/java      
tcp6       0      0 :::8080                 :::*                    LISTEN      1000       907499      25296/java      
$ ps 25296
  PID TTY      STAT   TIME COMMAND
25296 ?        Sl     0:16 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -Dcatalina.base=/hom

Use 'ps' and the number of the process that netstat reported back


You can know list of all ports running in system along with its details (pid, address etc.) :

netstat -tulpn

You can know details of a particular port number by providing port number in following command :

sudo netstat -lutnp | grep -w '{port_number}'

ex: sudo netstat -lutnp | grep -w '8080' Details will be provided like this :

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

if you want to kill a process using pid then : kill -9 {PID}

if you want to kill a process using port number : fuser -n tcp {port_number}

use sudo if you are not able to access any.


Best way to kill all processes on a specific port;

kill -9 $(sudo lsof -t -i:8080)

One liner

kill -9 $(lsof -t -i tcp:8080)

explanation here: use a combination of lsof and kill

root@localhost:~# lsof -i tcp:8080
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    23672 sine  238u  IPv6 3028222      0t0  TCP localhost:http-alt (LISTEN)

select pid and use kill

kill 23672

Other way with Git Bash:

stopProcessByPortNumber() {
port=":${1}"
portStrLine="$(netstat -ano | findstr LISTENING | findstr $port)"
processId="$(grep -oP '(\d+)(?!.*\d)' <<< $portStrLine)"
echo $processId
taskkill -PID $processId -F
}

This prints to stdout the process ids of everything running on <port_number>:

fuser -n tcp <port_number> 

It also prints some stuff to stderr, so:

fuser -n tcp <port_number> 2> /dev/null

We can then supply these process ids to the kill command:

sudo kill $(fuser -n tcp <port_number> 2> /dev/null)

You could also put this in a function if you do it a lot:

function killport() {
    sudo kill $(fuser -n tcp $1 2> /dev/null)
}

Linux: You can use this command if you know the port :

netstat -plten | grep LISTEN | grep 8080

AIX:

netstat -Aan | grep LISTEN | grep 8080

You then take the first column (example: f100050000b05bb8) and run the following command:

rmsock f100050000b05bb8 tcpcb

kill process.


try like this,

 sudo fuser -n tcp -k 8080

You can use the lsof command. Let port number like here is 8090

lsof -i:8090

This command returns a list of open processes on this port.

Something like…

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh 75782 eoin 5u IPv6 0x01c1c234 0t0 TCP localhost:8090 (LISTEN)

To free the port, kill the process using it(the process id is 75782)…

kill -9 75782

This one worked for me. here is the link from the original post: link


sudo apt-get install psmisc (or sudo yum install psmisc)
sudo fuser 80/tcp

Result: 80/tcp: 1858 1867 1868 1869 1871

Kill process one by one

kill -9 1858


Get the PID of the task and kill it.

lsof -ti:8080 | xargs kill

First you need to do is run (replace with your port number):

fuser -k 3000/tcp

This will release the port. After you run the above command run:

service docker restart

And your problem is resolved.


This fuser 8080/tcp will print you PID of process bound on that port.

And this fuser -k 8080/tcp will kill that process.

Works on Linux only. More universal is use of lsof -i4 (or 6 for IPv6).


Choose the port number and apply the grep in netstat command as shown below

netstat -ap | grep :7070

Console Output

tcp 0 0 :::7070 :::* LISTEN 3332/java

Kill the service based on PID ( Process Identification Number )

kill -9 3332

Run the following command to find 8080 port number PID:

sudo lsof -t -i:8080

You can now easily kill your PID using following command:

sudo kill -9

You can use one command to to kill a process on a specific port using the following command:

sudo kill -9 $(sudo lsof -t -i:8000)


To list any process listening to the port 8080:

lsof -i:8080

To kill any process listening to the port 8080:

kill $(lsof -t -i:8080)

or more violently:

kill -9 $(lsof -t -i:8080)

(-9 corresponds to the SIGKILL - terminate immediately/hard kill signal: see List of Kill Signals and What is the purpose of the -9 option in the kill command?. If no signal is specified to kill, the TERM signal a.k.a. -15 or soft kill is sent, which sometimes isn't enough to kill a process.).


In my case cent os has some issue in suggested answer. So I used following solution :

  ss -tanp | grep 65432 | head -1 | grep -Po "(?<=pid=).*(?=,)" | xargs kill

Simply run this command. Don't forget to replace portnumber, with your port ;)

  kill -9 $(sudo lsof -t -i:portnumber)

If you want to kill a process running on port number 8080 then first you need to find the 8080 port process identification number(PID) and then kill it. Run the following command to find 8080 port number PID:

sudo lsof -t -i:8080

Here,

  • sudo - command to ask admin privilege(user id and password).
  • lsof - list of files(Also used for to list related processes)
  • -t - show only process ID
  • -i - show only internet connections related process
  • :8080 - show only processes in this port number

So you can now easily kill your PID using following command:

sudo kill -9 <PID>

Here,

  • kill - command to kill the process
  • -9 - forcefully

You can use one command to to kill a process on a specific port using the following command:

sudo kill -9 $(sudo lsof -t -i:8080)

For more you can see the following link How to kill a process on a specific port on linux


Linux: First you can find PID of this command if you know the port :

netstat -tulpn 

example:-

 Local Address  Foreign Address  State    PID/Program name

  :::3000       :::*             LISTEN    15986/node 

You then take the kill process. run the following command:

kill -9 PID

Expample: -

kill -9 15986


To know the pid of service running on particular port :

netstat -tulnap | grep :*port_num*

you will get the description of that process. Now use kill or kill -9 pid. Easily killed.

e.g

netstat -ap | grep :8080

tcp6       0      0 :::8080       :::*    LISTEN      1880/java 

Now:

kill -9 1880

Remember to run all commands as root


  1. first check the process netstat -lt
  2. check process id fuser <port number>/tcp
  3. kill the process running on the port kill <process id>

How to kill process if the service on the port is not responding

timeout 1 telnet localhost 8080 2>&1 | if grep -q 'Unable'; then sudo kill -9 $(sudo lsof -t -i:8080); fi

What it does

  1. Start 'timeout' with one second, to make telnet exit
  2. ' 2>&1 | ' pipe error-message without exiting
  3. If error-message contains 'Unable', then run the kode to kill process locking port 8080

This can ble placed in a file and run regulary from 'sudo crontab'


A One-liner to kill only LISTEN on specific port:

kill -9 $(lsof -t -i:3000 -sTCP:LISTEN)


I'm working on a Yocto Linux system that has a limited set of available Linux tools. I wanted to kill the process that was using a particular port (1883).

First, to see what ports we are listening to I used the following command:

root@root:~# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:hostmon         0.0.0.0:*               LISTEN      
tcp        0      0 localhost.localdomain:domain 0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:9080            0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      
tcp        0      0 :::hostmon              :::*                    LISTEN      
tcp        0      0 localhost:domain        :::*                    LISTEN      
tcp        0      0 :::ssh                  :::*                    LISTEN      
tcp        0      0 :::1883                 :::*                    LISTEN      

Next, I found the name of the process using port 1883 in the following way:

root@root:~# fuser 1883/tcp
290 
root@root:~# ps | grep 290
  290 mosquitt 25508 S    /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
12141 root      8444 S    grep 290

As we can see above, it's the program /usr/sbin/mosquitto that's using port 1883.

Lastly, I killed the process:

root@root:~# systemctl stop mosquitto

I used systemctl becuase in this case it was a systemd service.


kill -9 `fuser 8080/tcp|xargs -n 1`, this commands also kills the process that listens on port 8080 with TCP connection


In Windows, it will be netstat -ano | grep "8080" and we get the following message TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 10076

WE can kill the PID using taskkill /F /PID 10076


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to unix

Docker CE on RHEL - Requires: container-selinux >= 2.9 What does `set -x` do? How to find files modified in last x minutes (find -mmin does not work as expected) sudo: npm: command not found How to sort a file in-place How to read a .properties file which contains keys that have a period character using Shell script gpg decryption fails with no secret key error Loop through a comma-separated shell variable Best way to find os name and version in Unix/Linux platform Resource u'tokenizers/punkt/english.pickle' not found

Examples related to port

Docker - Bind for 0.0.0.0:4000 failed: port is already allocated How do I kill the process currently using a port on localhost in Windows? Node.js Port 3000 already in use but it actually isn't? Can't connect to Postgresql on port 5432 Spring Boot - How to get the running port Make docker use IPv4 for port binding How to change the default port of mysql from 3306 to 3360 Open firewall port on CentOS 7 Unable to launch the IIS Express Web server, Failed to register URL, Access is denied XAMPP Port 80 in use by "Unable to open process" with PID 4

Examples related to kill-process

How to find MySQL process list and to kill those processes? Kill tomcat service running on any port, Windows Closing Excel Application Process in C# after Data Access linux script to kill java process How to kill a process running on particular port in Linux? Kill a Process by Looking up the Port being used by it from a .BAT How to terminate a python subprocess launched with shell=True Kill some processes by .exe file name