How to find a used TCP port and kill the associated process on your Mac
Published: 2019-01-30 • Last updated: 2019-09-12
Sometimes the port you need is already used by another process. To find the offending process:
sudo lsof -i tcp:<port>
Now, usually you’d just close out the app that was using the port. But let’s say you’re feeling destructive and you wanna kill the process:
sudo kill <pid>
For fun, the above commands can be combined into this one-liner:
sudo kill $(sudo lsof -i tcp:<port> | tr -s " " | cut -d " " -f 2 | sed -n 2p)
Credits to this stackoverflow post.