#!/bin/sh usage() { cat >&2 << EOF killport: kill -9 any process that is bound to a local port Usage: killport EOF } case "$1" in "-h") usage && exit 0 ;; "--help") usage && exit 0 ;; esac PORT=$1 [ -z "$PORT" ] && echo "killport: Please provide a !" && usage && exit 1 >&2 [ "$PORT" -lt 1 ] && echo "killport: port must be a postive integer!" && usage && exit 1 >&2 PID=$(lsof -i tcp:$PORT | tail -n1 | awk '{print $2}') NAME=$(lsof -i tcp:$PORT | tail -n1 | awk '{print $1}') [ -z "$PID" ] && echo "No process bound to $PORT, doing nothing" && exit 0 >&2 echo "Killing process $NAME (PID: $PID) that is bound to local port $PORT" kill -9 $PID