Last active
May 16, 2021 20:24
-
-
Save p-karanthaker/d9250a1b141e429a275a072d32ce37a1 to your computer and use it in GitHub Desktop.
Kills the process which is using a given port
This file contains hidden or 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 | |
re='^[0-9]+$' | |
if ! [[ $1 =~ $re ]] ; then | |
echo "error: Port must be a number" >&2; exit 1 | |
fi | |
pids=( $(sudo lsof -ti:$1) ) | |
if [[ ! -z "$pids" ]] | |
then | |
for pid in "$pids"; do | |
echo "Killing process $pid" | |
sudo kill -9 "$pid" | |
done | |
else | |
echo "Port $1 not in use" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment