Created
February 21, 2018 11:15
-
-
Save m5r/0eb4dd251d719f670f341768e6b06ec7 to your computer and use it in GitHub Desktop.
Revisions
-
m5r created this gist
Feb 21, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ #!/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 }