Skip to content

Instantly share code, notes, and snippets.

@eneuhauser
Created November 21, 2017 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eneuhauser/f23293a3bfd6d0ad777827ea4f837cac to your computer and use it in GitHub Desktop.
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.
#! /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
@eneuhauser
Copy link
Author

I have the script in a bin folder on my path and have the following alias in my zshrc.

alias kill_dc1="kill_server.sh node 8088 8089 9088"

@seeroush
Copy link

The Lord's work, you do. 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment