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

1,886,922

Solution 1

Use the command

 sudo netstat -plten |grep java

used grep java as tomcat uses java as their processes.

It will show the list of processes with port number and process id

tcp6       0      0 :::8080                 :::*                    LISTEN      
1000       30070621    16085/java

the number before /java is a process id. Now use kill command to kill the process

kill -9 16085

-9 implies the process will be killed forcefully.

Solution 2

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).

Solution 3

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.).

Solution 4

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

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

Option 2 If you have npm installed you can also run

npx kill-port 3000

Solution 5

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
Share:
1,886,922
veer7
Author by

veer7

Oracle Certified Java Professional / OCJP6 Oracle Certified Java Professional - Java Web Content Developer / OCPJWCD SCRUM Alliance - Certified Scrum Master SCRUM Alliance - Scrum Certified Product Owner

Updated on April 27, 2022

Comments

  • veer7
    veer7 about 2 years

    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.

  • Jonas Schäfer
    Jonas Schäfer almost 12 years
    You might want to add that one might need root privilegues to get process names via netstat.
  • fer y
    fer y over 10 years
    does this close a possible connected socket to 8080 as well?
  • nudzo
    nudzo over 10 years
    Of course, all file descriptors are closed when proces finish.
  • veer7
    veer7 about 9 years
    to kill process forcefully you will need to use -9 like kill -9 75782; as sometimes few processes aren't kill with just kill
  • Mark Lakata
    Mark Lakata over 8 years
    It does not properly close the port. The port is put into TIME_WAIT state after the parent process is killed. The OS will then eventually completely close the port after about 60 seconds. It means that you can't reuse the port for at least 60 seconds (unless you give the reuse option to the socket).
  • abcd
    abcd over 7 years
    @JonasWielicki you can see the ones you own w/out root privileges.
  • a p
    a p over 7 years
    On Darwin, must be using a different version of fuser. Only takes a file, doesn't support -k.
  • babycakes
    babycakes over 7 years
    The question is clearly asking for Linux help; not Windows.
  • Hesham Yassin
    Hesham Yassin about 7 years
    'fuser -k -n tcp 8080' will kill the process too
  • Sudhir Belagali
    Sudhir Belagali about 7 years
    kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] This is what I am getting when I Execute your commands
  • Marcel Djaman
    Marcel Djaman almost 7 years
    @SudhirBelagali did you it with super user sudo
  • JesseBoyd
    JesseBoyd over 6 years
    I needed root privileges for netstat. Is there a way to actually know what the process came from? As far as I could tell I had closed all applications but it may have been from a terminal window where I inadvertently pushed the processes to a background task. Maybe I should have ran the terminal command 'ps' to see all processes first...
  • asherbret
    asherbret over 6 years
    Worked on my Mac as well
  • Robin-Hoodie
    Robin-Hoodie over 6 years
    I just noticed that I downvoted this by accident a few weeks ago, if you edit your anwer slightly I should be able to remove it
  • Sam Kah Chiin
    Sam Kah Chiin about 6 years
    I think this should be the best answer. Thanks @Gal Bracha
  • Akavall
    Akavall over 5 years
    I would like to add that sometimes you do not have permissions to see the process id, in that case you need to do sudo lsof -i:8080.
  • Webwoman
    Webwoman over 5 years
    why the second tips is more violent ?
  • Russo
    Russo over 5 years
    This is the best solution because it does not kill my browser as well!
  • Russo
    Russo over 5 years
    @Webman more violently means it kills my web browser at well :( Here is the best solution from Gal Bracha: kill -9 $(lsof -t -i:3000 -sTCP:LISTEN)
  • Franck Dernoncourt
    Franck Dernoncourt over 5 years
    @Webman -9 corresponds to SIGKILL - terminate immediately/hard kill: see List of Kill Signals and What is the purpose of the -9 option in the kill command?)
  • Junaid
    Junaid over 5 years
    thanks mate. sudo kill -9 $(sudo lsof -t -i:8080) worked for me
  • Tom
    Tom over 5 years
    @dbliss fuser is part of psmisc. In case you get fuser: command not found, install psmisc. For CentOS/RHEL 7, run sudo yum install psmisc
  • AMIC MING
    AMIC MING over 4 years
    Thanks for the answer. Very smoothly works in Mac OS. You are the best. Just Up Vote.
  • dvlsg
    dvlsg about 4 years
    Works well with port ranges, too, like lsof -ti:8080-8089 | xargs kill.
  • Kuldeep Yadav
    Kuldeep Yadav about 4 years
    doesn't sudo prompt for password
  • Robert
    Robert almost 4 years
    The question asks about Linux.
  • Aditya Parmar
    Aditya Parmar almost 4 years
    No need sudo in mac most of the time.
  • Arthur
    Arthur over 3 years
    Based on this answer I created a function into ~/.bashrc: killp() { kill -9 $(lsof -t -i:"$1" -sTCP:LISTEN) } in order to be able to use it like this killp 3000. (Do not forget to add new lines for { })
  • MikeF
    MikeF over 3 years
    sudo netstat -plten | grep :8080 will work even when you do not know the application. This found a ruby process that lsof -i did not find for me.
  • perymerdeka
    perymerdeka about 3 years
    good answer thanks it also work for me too
  • Jose Mhlanga
    Jose Mhlanga about 3 years
    Awesome, this is the command that worked for me, Ubuntu
  • Anshuman Bardhan
    Anshuman Bardhan over 2 years
    Thanks guys, appreciated 🙂
  • Pablo Bianchi
    Pablo Bianchi over 2 years
    netstat is the deprecated alternative to ss. Here an example on how to use it.
  • Hadisur Rahman
    Hadisur Rahman over 2 years
    solved my problem
  • Sava B.
    Sava B. over 2 years
    You are a nice person, thank you
  • Danny
    Danny about 2 years
    Nit: syntax error for the extra `
  • Dannie
    Dannie about 2 years
    lsof -t -i:8080 | xargs -r kill handles the empty list case better.
  • Ben Gooding
    Ben Gooding almost 2 years
    I needed sudo kill $(sudo lsof -t -i:80)