How to Kill a Process Running on Particular Port in Linux

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

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.

How to kill a process on a port on ubuntu

You want to use backtick, not regular tick:

sudo kill -9 `sudo lsof -t -i:9001`

If that doesn't work, you could also use $() for command interpolation:

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

How to find processes based on port and kill them all?

The problem with ps -efl | grep PORT_NUMBER is that PORT_NUMBER may match other columns in the output of ps as well (date, time, pid, ...). A potential killing spree if run by root!

I would do this instead :

PORT_NUMBER=1234
lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill

Breakdown of command

  • (lsof -i tcp:${PORT_NUMBER}) -- list all processes that is listening on that tcp port
  • (awk 'NR!=1 {print $2}') -- ignore first line, print second column of each line
  • (xargs kill) -- pass on the results as an argument to kill. There may be several.

How do I kill the process currently using a port on localhost in Windows?

Step 1:

Open up cmd.exe (note: you may need to run it as an administrator, but this isn't always necessary), then run the below command:

netstat -ano | findstr :<PORT>

(Replace <PORT> with the port number you want, but keep the colon)

Sample Image

The area circled in red shows the PID (process identifier). Locate the PID of the process that's using the port you want.

Step 2:

Next, run the following command:

taskkill /PID <PID> /F

(No colon this time)

Sample Image

Lastly, you can check whether the operation succeeded or not by re-running the command in "Step 1". If it was successful you shouldn't see any more search results for that port number.

How to kill processes running on a port only if the port is open?

If I understand your question correctly, the output of lsof -i:9005 -t will by definition be empty when the port is not open. xargs on Linux has an option for handling this:

sudo lsof -i:9005 -t | xargs -r sudo kill

or you can simply check the exit code of lsof; it will be non-zero to signal an error if it didn't find anything to report:

if pids=$(sudo lsof -i:9005 -t); then
sudo kill $pids
fi

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

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.

shell script to kill the process listening on port 3000?

alias kill3000="fuser -k -n tcp 3000"


Related Topics



Leave a reply



Submit