How to kill a process by port on MacOS, a la fuser -k 9000/tcp

14

7

On linux I can kill a process knowing only the port it is listening on using fuser -k 9000/tcp, how do I so the same on MacOS?

Kris

Posted 2012-07-19T15:41:35.483

Reputation: 425

Answers

19

lsof -P | grep ':PortNumber' | awk '{print $2}' | xargs kill -9

Change PortNumber to the actual port you want to search for.

stefano

Posted 2012-07-19T15:41:35.483

Reputation: 206

2I just had to add -9 to the end to get this to work, but I believe that is due to the nature of the listening application and not generally recommended practice, to kill -9 that is. – Kris – 2012-07-24T07:37:15.353

@Kris - lsof -P | grep ':NumberOfPort' | awk '{print $2}' | xargs kill -9 worked! – aces. – 2013-01-18T15:56:09.820

11

Adding the -t and -i flags to lsof should speed it up even more by removing the need for grep and awk.

lsof -nti:NumberOfPort | xargs kill -9

Zlemini

Posted 2012-07-19T15:41:35.483

Reputation: 291

2Works and is more concise than the accepted answer! – Big Rich – 2017-07-11T15:09:24.623

1WAY faster with this approach – daleyjem – 2020-01-27T16:14:26.703

2

Add -n to lsof and you remove the reverse DNS lookup from the command and reduce the run time from minutes to seconds.

lsof -Pn | grep ':NumberOfPort' | awk '{print $2}' | xargs kill -9

stevehollx

Posted 2012-07-19T15:41:35.483

Reputation: 21

1

  1. Check your port is open or not by

sudo lsof -i : {PORT_NUMBER}

COMMAND PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    582 Thirumal  300u  IPv6 0xf91b63da8f10f8b7      0t0  TCP *:distinct (LISTEN)

2. Close the port by killing process PID

sudo kill -9 582

Thirumal

Posted 2012-07-19T15:41:35.483

Reputation: 111

1

You can see if a port if open by this command

 sudo lsof -i :8000

where 8000 is the port number

If the port is open, it should return a string containing the Process ID (PID).

Copy this PID and

kill -9 PID

If you need to see all the open ports, you can perform a Port Scan in the Network Utility application.

rohanharikr

Posted 2012-07-19T15:41:35.483

Reputation: 11

0

You can use kill -9 $(lsof -i:PORT -t) 2> /dev/null, where PORT is your actual port number. It will kill the process which is running on your given port.

Epk

Posted 2012-07-19T15:41:35.483

Reputation: 1

You are repeating other answer – yass – 2017-06-27T12:14:11.113