Last active
May 28, 2019 15:20
-
-
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 ;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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