Stop Port in Terminal

Stop running port in mac terminal

Velu S Gautam1 min readLearnings

I frequently run into processes hogging a specific port on macOS. Here's a handy command to kill whatever's occupying a port from the terminal:

stop portnumber

BASH
pid=$(lsof -ti tcp:$1)
if [[ $pid ]]; then
  kill -9 $pid
  echo "Congrats!! $1 is stopped."
else
   echo "Sorry nothing running on above port" 
fi

To set this up, navigate to the directory and create a file named "stop" using Vim:

BASH
cd /usr/local/bin/
vim stop

Grant execution permission to the "stop" file:

BASH
chmod +x /usr/local/bin/stop

It's done. 💡

Now restart the terminal. You can then use the command stop <port_number> to kill the process running on that port.

Your setup is now complete. 🎉

Cheers ✌🏻

Comments