Last active
July 19, 2022 15:58
-
-
Save ejach/9d4de2b050dc8058fe245d59bd28d67d to your computer and use it in GitHub Desktop.
Kill a program based off the port by PID
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
#!/usr/bin/env bash | |
# Usage murk <port_number> | |
if [[ $# -eq 1 ]]; then | |
pid_array=( ` lsof -i:$1 -t ` ) | |
pid_array_length=${#pid_array[@]} | |
if [[ "$pid_array_length" -eq "0" ]]; then | |
echo "No PID's on that port number have been found" | |
fi | |
for element in "${pid_array[@]}" | |
do | |
i=0 | |
while [ $((i)) -lt $((pid_array_length)) ]; do | |
echo $i: ` lsof -i:$1 | tail -n +2 | awk '{print $1,$2}' ` | |
((i++)) | |
if [[ $((i)) -eq $((pid_array_length)) ]]; then | |
if [[ $((pid_array_length)) -lt 2 ]]; then | |
read -p "Do you wish to kill ${pid_array[0]}? (y/n): " yn | |
case $yn in | |
y ) kill ${pid_array[0]} | |
echo "Process ${pid_array[0]} has been killed" | |
exit 1;; | |
n ) echo "Program aborted"; | |
exit;; | |
* ) echo "Invalid response, please try again"; | |
exit 1;; | |
esac | |
else | |
read -p "Enter what number you wish to execute, q for quit: " sel2 | |
if [[ $sel2 = "q" ]]; then | |
exit 1 | |
elif [[ $((sel2)) -lt $((pid_array_length)) ]]; then | |
kill ${pid_array[$sel2]} | |
echo "Process ${pid_array[$sel2]} has been killed" | |
exit 1 | |
else | |
echo "Incorrect input, please try again." | |
fi | |
fi | |
fi | |
done | |
done | |
else | |
printf "Insufficient amount of arguments \n Usage: \n murk <port_number>\n" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment