Shell function to find and kill server processes

MAY 16, 2022

Add these neat little functions in your .zshrc file and use them to quickly find what ports are in use and kill them off.

listenfor() {
  if [ "$1" != "" ]
  then
    lsof -wni tcp:"$1"
  else
    echo "need to enter a port number (i.e. 3000)"
  fi
}

killpid() {
  if [ "$1" != "" ]
  then
    kill -9 "$1"
  else
    echo "need to enter PID number"
  fi
}

Now you can run something like: listenfor 3000 and killpid 71234.

— Adrian