Last active
April 30, 2022 08:22
-
-
Save robertolos/903b6f72bf83336909892f15aff67cb6 to your computer and use it in GitHub Desktop.
Kills process running on port
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
# 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