Skip to content

Instantly share code, notes, and snippets.

@BohdanLevchenko
Created August 1, 2020 12:34
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 BohdanLevchenko/586fd92b920cf320f854b6e73066efbd to your computer and use it in GitHub Desktop.
Save BohdanLevchenko/586fd92b920cf320f854b6e73066efbd to your computer and use it in GitHub Desktop.
kill-by-port.sh
#!/bin/bash
set -eu
declare -r PORT=$1
# kills process listening on specified port
if [ -z "$PORT" ]; then
echo Missing mandatory PORT attirbute!
exit 1
fi
declare -r PID=$(netstat -abno | grep 0.0.0.0:$PORT | awk -F " " '{print $5}')
if [ ! -z "$PID" ]; then
echo Found PID: $PID listening on port $PORT
# NOTE: use /bin/kill - http://superuser.com/questions/439539/how-to-kill-a-windows-process-in-a-cygwin-terminal
/bin/kill -f -9 $PID
echo Process $PID killed
exit 0
else
echo Unable to find process listening on port $PORT
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment