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

35

1460

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.

Melodize answered 20/7, 2012 at 16:39 Comment(2)
possible duplicate of shell script to kill the process listening on port 3000?Invincible
Just warning for busybox users: With busybox 'lsof', those proposed kill $(lsof ...) commands will kill lot more that you want.Marilumarilyn
M
390

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.

Melodize answered 20/7, 2012 at 16:39 Comment(6)
You might want to add that one might need root privilegues to get process names via netstat.Trudey
@JonasWielicki you can see the ones you own w/out root privileges.Mo
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...Mueller
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.Latterday
netstat is the deprecated alternative to ss. Here an example on how to use it.Ailin
possible TOCTOUC issue depending on your use caseTrimetrogon
M
1735

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

General form:

# list the TCP process bound to port PORT
fuser PORT/tcp
# Example: list the TCP process bound to port 8080
fuser 8080/tcp

# list the UDP process bound to port PORT
fuser PORT/udp
# Example: list the UDP process bound to port 8080
fuser 8080/udp
Myranda answered 21/7, 2012 at 21:57 Comment(8)
does this close a possible connected socket to 8080 as well?Muraida
Of course, all file descriptors are closed when proces finish.Myranda
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).Synchronous
On Darwin, must be using a different version of fuser. Only takes a file, doesn't support -k.Sparing
'fuser -k -n tcp 8080' will kill the process tooSouthward
@dbliss fuser is part of psmisc. In case you get fuser: command not found, install psmisc. For CentOS/RHEL 7, run sudo yum install psmiscBradstreet
There is a TOCTOU issue with using lsof and kill, depending on what you're doing (not an issue in the case of an SSH tunnel you picked the port for and only one instance of your script is running). I'm not sure if fuser avoids it - it may be platform dependent.Trimetrogon
if the task is bind to root user need to run as sudoGarpike
J
1405

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

Jacobsohn answered 15/9, 2015 at 18:17 Comment(12)
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] This is what I am getting when I Execute your commandsInterpose
@SudhirBelagali did you it with super user sudoFold
Worked on my Mac as wellDeanedeaner
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.Measures
why the second tips is more violent ?Stilla
@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)Bohon
@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?)Jacobsohn
lsof -t -i:8080 | xargs -r kill handles the empty list case better.Hybris
I needed sudo kill $(sudo lsof -t -i:80)Avidity
how to prevent it from restart ? After hard kill process restarts again. tcp6 0 0 :::9100 :::* LISTEN 57131/node_exporterFavoritism
There is a TOCTOU issue with using lsof and kill, depending on what you're doing (not an issue in the case of an SSH tunnel you picked the port for and only one instance of your script is running). I'm not sure if fuser avoids it - it may be platform dependent.Trimetrogon
the middle kill $(lsof -t -i:8080) worked for me, Thanks! (MBP M1Pro)Reach
M
390

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.

Melodize answered 20/7, 2012 at 16:39 Comment(6)
You might want to add that one might need root privilegues to get process names via netstat.Trudey
@JonasWielicki you can see the ones you own w/out root privileges.Mo
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...Mueller
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.Latterday
netstat is the deprecated alternative to ss. Here an example on how to use it.Ailin
possible TOCTOUC issue depending on your use caseTrimetrogon
L
364

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
Lysis answered 22/1, 2017 at 13:44 Comment(3)
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 { })Uniaxial
Nit: syntax error for the extra `Forebode
possible TOCTOU issue depending on the use caseTrimetrogon
L
256

One liner, a time saver

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
Lennalennard answered 26/11, 2019 at 16:30 Comment(0)
Y
150

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

Yardmaster answered 11/2, 2015 at 10:29 Comment(1)
to kill process forcefully you will need to use -9 like kill -9 75782; as sometimes few processes aren't kill with just killMelodize
P
97

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

Paracelsus answered 18/5, 2018 at 12:29 Comment(2)
thanks mate. sudo kill -9 $(sudo lsof -t -i:8080) worked for meInjury
Awesome, this is the command that worked for me, UbuntuZendejas
T
83

Best way to kill all processes on a specific port;

kill -9 $(sudo lsof -t -i:8080)
Tractile answered 4/12, 2019 at 12:57 Comment(2)
doesn't sudo prompt for passwordGeophyte
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]. It seems to -l is missing as recommended kill -l -9 $(sudo lsof -t -i:8983)Sexless
A
51

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)
}
Agentive answered 27/9, 2013 at 21:10 Comment(0)
C
45

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

Cruzcruzado answered 12/8, 2014 at 8:59 Comment(0)
G
42

Get the PID of the task and kill it.

lsof -ti:8080 | xargs kill
Gruchot answered 29/11, 2018 at 4:37 Comment(2)
Thanks for the answer. Very smoothly works in Mac OS. You are the best. Just Up Vote.Phalanger
Works well with port ranges, too, like lsof -ti:8080-8089 | xargs kill.Liquidity
T
36

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.

Thimbleful answered 21/7, 2020 at 10:2 Comment(2)
good answer thanks it also work for me tooCzech
Thanks guys, appreciated 🙂Thimbleful
P
29

try like this,

 sudo fuser -n tcp -k 8080
Peruke answered 26/6, 2015 at 11:15 Comment(0)
N
25

This will kill programs running on port 80

sudo fuser -k 80/tcp
Nittygritty answered 17/6, 2021 at 12:11 Comment(1)
correct but redundant - identical to an above answerTrimetrogon
H
23

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
Hoagy answered 6/8, 2019 at 2:55 Comment(0)
M
19

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)

Millibar answered 17/6, 2020 at 7:12 Comment(0)
M
16

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.

Mesencephalon answered 14/3, 2019 at 9:50 Comment(0)
D
15
  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

Desai answered 16/12, 2015 at 9:26 Comment(1)
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 itDriftwood
G
15

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

Gilolo answered 7/3, 2018 at 13:12 Comment(0)
L
14

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.

Lombard answered 12/8, 2014 at 7:14 Comment(0)
A
10

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

  kill -9 $(sudo lsof -t -i:portnumber)
Abbie answered 13/6, 2020 at 22:2 Comment(1)
No need sudo in mac most of the time.Militate
K
7

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

Kastner answered 22/5, 2017 at 5:59 Comment(0)
S
6
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

Spidery answered 4/6, 2018 at 15:44 Comment(0)
S
6

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
Stalky answered 14/7, 2020 at 16:8 Comment(1)
The question asks about Linux.Contrariety
H
5

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.

Holdfast answered 5/5, 2020 at 4:43 Comment(0)
R
4
  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>
Rudolph answered 24/5, 2020 at 10:52 Comment(0)
D
3

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
}
Delacruz answered 9/2, 2019 at 18:38 Comment(0)
T
3

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
Taunton answered 10/2, 2020 at 6:38 Comment(0)
C
3

Just this commands work for me:

ps -aux | grep 8000

and then:

sudo kill <PID>
Chema answered 26/4, 2022 at 10:37 Comment(1)
ps -aux including following headers, and port is not one of them: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDCorcovado
M
3

My personal way to solve this issue can be found is this gist I made, in the gist the first step is to run:

sudo lsof -i [PROTOCAL]:[PORT]

And then run:

sudo kill -9 [PROCESS ID]
Manvell answered 18/11, 2023 at 19:51 Comment(0)
M
2

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

Mueller answered 12/10, 2017 at 2:15 Comment(0)
P
2

This worked for me in mac

sudo kill -9 $(netstat -vnap tcp | grep PORT | awk -F " " '{print $9}')
Perkoff answered 25/8, 2022 at 16:0 Comment(0)
L
1

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'

Latialatices answered 31/8, 2020 at 11:14 Comment(0)
A
1

Using this script, which is more user-friendly than other options and checks that the port is actually in use.

Usage: openport 1234

openport() {
  # sed = second line https://mcmap.net/q/46210/-command-to-get-nth-line-of-stdout
  # awk = second word https://unix.stackexchange.com/a/174038
  processId=$(lsof -i tcp:$1 | sed -n 2p | awk '{print $2}')
  if [ ! -z "$processId" ] # Non-null https://www.cyberciti.biz/faq/bash-shell-find-out-if-a-variable-has-null-value-or-not/
  then
    echo Killing $processId
    kill -9 $processId
  else
    echo No process found
  fi
}
Armada answered 18/5, 2022 at 17:38 Comment(0)
T
-1

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

Tanh answered 28/9, 2016 at 5:36 Comment(1)
The question is clearly asking for Linux help; not Windows.Delative

© 2022 - 2024 — McMap. All rights reserved.