Find the process running on a port on your mac

Some­times you just wanna kill an old process thats still lis­ten­ing on a port.

** TLDR: Here’s a one liner to find and kill a process on a given port (in the below exam­ple, 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 run­ning a port on your mac by run­ning either of the fol­low­ing com­mands. 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 prob­a­bly want to know the process cor­re­spond­ing to the PID, just in case you are the curi­ous kind.

ps -a $PID

If you then need to kill that process

kill -9 $PID

Ref­er­ences

  • 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

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...