Created
February 21, 2018 11:15
-
-
Save m5r/0eb4dd251d719f670f341768e6b06ec7 to your computer and use it in GitHub Desktop.
This file contains 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 | |
if [ -z "$1" ]; then | |
echo "Error: No argument passed" >&2; exit 1 | |
fi | |
if ! [[ $1 =~ ^[0-9]+$ ]] ; then | |
echo "Error: Argument is not a number" >&2; exit 1 | |
fi | |
PIDToKill=$(lsof -i:$1 | grep LISTEN | awk '{print $2}' | uniq) | |
if [ -z "$PIDToKill" ]; then | |
echo "Error: No process listening on port ${1} found" >&2; exit 1 | |
fi | |
{ | |
kill -9 $PIDToKill | |
echo "Successfully killed process with PID ${PIDToKill} listening on port ${1}"; exit 1 | |
} || { | |
echo "Error: unknown error when tried to kill process ${PIDToKill}" >&2; exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment