Created
August 1, 2020 12:34
-
-
Save BohdanLevchenko/586fd92b920cf320f854b6e73066efbd to your computer and use it in GitHub Desktop.
kill-by-port.sh
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 | |
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