August 28, 2024

Simple ZSH Utility to Kill a Port


Overview

Whenever something bad happens (generally Intellij crashing), I find myself trying to restart applications and often seeing this error:

Then, I'd proceed to google How to find and kill port, which always led me to the many responses on Stack Overflow

Solution

I added this to my ~/.zshrc and never looked back.

killport() {
  for port in "$@"; do
    lsof -ti:$port | xargs kill -9
  done
}

alias kp="killport"
zshrc

which means you can:

killport 3000
killport 3000 8080
zsh

or for brevity :)

kp 3000
kp 3000 8080
zsh