How to kill a process running on particular port in Linux?
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
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, 2022Comments
-
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 port8080
.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.-
Ciro Santilli OurBigBook.com over 9 yearspossible duplicate of shell script to kill the process listening on port 3000?
-
-
Jonas Schäfer almost 12 yearsYou might want to add that one might need root privilegues to get process names via
netstat
. -
fer y over 10 yearsdoes this close a possible connected socket to 8080 as well?
-
nudzo over 10 yearsOf course, all file descriptors are closed when proces finish.
-
veer7 about 9 yearsto 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 over 8 yearsIt 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 over 7 years@JonasWielicki you can see the ones you own w/out root privileges.
-
a p over 7 yearsOn Darwin, must be using a different version of
fuser
. Only takes a file, doesn't support-k
. -
babycakes over 7 yearsThe question is clearly asking for Linux help; not Windows.
-
Hesham Yassin about 7 years'fuser -k -n tcp 8080' will kill the process too
-
Sudhir Belagali about 7 yearskill: 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 almost 7 years@SudhirBelagali did you it with super user
sudo
-
JesseBoyd over 6 yearsI 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 over 6 yearsWorked on my Mac as well
-
Robin-Hoodie over 6 yearsI 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 about 6 yearsI think this should be the best answer. Thanks @Gal Bracha
-
Akavall over 5 yearsI 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 over 5 yearswhy the second tips is more violent ?
-
Russo over 5 yearsThis is the best solution because it does not kill my browser as well!
-
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 over 5 years@Webman
-9
corresponds toSIGKILL - terminate immediately/hard kill
: see List of Kill Signals and What is the purpose of the -9 option in the kill command?) -
Junaid over 5 yearsthanks mate.
sudo kill -9 $(sudo lsof -t -i:8080)
worked for me -
Tom over 5 years@dbliss fuser is part of psmisc. In case you get
fuser: command not found
, install psmisc. For CentOS/RHEL 7, runsudo yum install psmisc
-
AMIC MING over 4 yearsThanks for the answer. Very smoothly works in Mac OS. You are the best. Just Up Vote.
-
dvlsg about 4 yearsWorks well with port ranges, too, like
lsof -ti:8080-8089 | xargs kill
. -
Kuldeep Yadav about 4 yearsdoesn't sudo prompt for password
-
Robert almost 4 yearsThe question asks about Linux.
-
Aditya Parmar almost 4 yearsNo need sudo in mac most of the time.
-
Arthur over 3 yearsBased 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 thiskillp 3000
. (Do not forget to add new lines for{ }
) -
MikeF over 3 years
sudo netstat -plten | grep :8080
will work even when you do not know the application. This found a ruby process thatlsof -i
did not find for me. -
perymerdeka about 3 yearsgood answer thanks it also work for me too
-
Jose Mhlanga about 3 yearsAwesome, this is the command that worked for me,
Ubuntu
-
Anshuman Bardhan over 2 yearsThanks guys, appreciated 🙂
-
Pablo Bianchi over 2 years
-
Hadisur Rahman over 2 yearssolved my problem
-
Sava B. over 2 yearsYou are a nice person, thank you
-
Danny about 2 yearsNit: syntax error for the extra `
-
Dannie about 2 years
lsof -t -i:8080 | xargs -r kill
handles the empty list case better. -
Ben Gooding almost 2 yearsI needed
sudo kill $(sudo lsof -t -i:80)