Created
November 21, 2017 18:16
-
-
Save eneuhauser/f23293a3bfd6d0ad777827ea4f837cac to your computer and use it in GitHub Desktop.
Script to kill applications listening to a port(s). Requires at least 2 parameters, the application and port, but could take in additional ports if more than one needs to be killed.
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 | |
PROC=$1 | |
if [[ "$#" -lt 2 ]]; then | |
echo "Requires at least 2 arguments, the process name and at least one port number." | |
exit 1 | |
fi | |
for ip in "$@" | |
do | |
if [[ "$ip" -ne "$PROC" ]]; then | |
echo "Stopping $PROC listening to $ip..." | |
lsof -i ":$ip" | grep "$PROC" | awk '{print $2}' | xargs -L 1 kill | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have the script in a bin folder on my path and have the following alias in my zshrc.