• 0

Find the process running on a port on your mac

Sometimes you just wanna kill an old process thats still listening on a port. ** TLDR: Here's a one liner to find and kill a process on a given port (in the below example, port = 8080) if you already know what that process is **

lsof -n -i4TCP:8080 | grep LISTEN | awk '{print $2}' | xargs kill -9

Breakown of the commands

Get the process id or PID of the process running a port on your mac by running either of the following commands. Replace $PORT with the port you wish to examine.
lsof -n -i4TCP:$PORT | grep LISTEN
lsof -n -iTCP:$PORT | grep LISTEN
lsof -n -i:$PORT | grep LISTEN
Once you have the PID, you probably want to know the process corresponding to the PID, just in case you are the curious kind.
ps -a $PID
If you then need to kill that process
kill -9 $PID

References

  • https://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x
  • https://superuser.com/questions/632979/if-i-know-the-pid-number-of-a-process-how-can-i-get-its-name