Skip to content

Instantly share code, notes, and snippets.

@bazay
Last active May 28, 2019 15:20
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 bazay/9b340a0c76d8e564bbc0555eefd68ee2 to your computer and use it in GitHub Desktop.
Save bazay/9b340a0c76d8e564bbc0555eefd68ee2 to your computer and use it in GitHub Desktop.
A helpful Bash script that allows you to find a process running on a particular port and terminate it. Particularly useful for us web developers ;)
#!/bin/bash
#
# kill_port PORT
#
# Find process running on a particular port and terminate it.
kill_port () {
if [[ -z "$1" ]]; then
echo 'Please specify PORT arg, e.g:'
echo ' kill_port PORT'
return 1
fi
PID="$(sudo lsof -i tcp:$1 | awk 'FNR == 2 {print $2}')"
if [[ -z "$PID" ]]; then
echo "No process found running on port $1"
return 1
fi
kill -9 $PID
echo "Killed process $PID on port $1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment