Skip to content

Instantly share code, notes, and snippets.

@robertolos
Last active April 30, 2022 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertolos/903b6f72bf83336909892f15aff67cb6 to your computer and use it in GitHub Desktop.
Save robertolos/903b6f72bf83336909892f15aff67cb6 to your computer and use it in GitHub Desktop.
Kills process running on port
# Add to .bashrc or .zshrc
psportkill() {
if [ $# -eq 0 ]
then
echo "Missing port number. Usage: psportkill 5000"
exit 1
fi
echo "Killing process on port $1"
lsof -i 4tcp:$1 -sTCP:LISTEN | awk '{if(NR>1)print $2}' | xargs kill
if [ $? -eq 0 ]
then
echo "Process killed."
exit 0
else
echo "Failure: no process found running on port $1" >&2
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment