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?
Asked
Active
Viewed 1.3e+01k times
7 Answers
25
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
lsof arguments:
-nAvoids host names lookup (may result in faster performance)-tTerse output; returns process IDs only to facilitate piping the output to kill-iSelects only those files whose Internet address matches
kill arguments:
-9Non-catchable, non-ignorable kill
4
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
- 141
3
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
- 31
2
- 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
- 123
- 3
1
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.
Nathan.Eilisha Shiraini
- 2,674
Epk
- 11