Skip to content

Instantly share code, notes, and snippets.

@p-karanthaker
Last active May 16, 2021 20:24
Show Gist options
  • Save p-karanthaker/d9250a1b141e429a275a072d32ce37a1 to your computer and use it in GitHub Desktop.
Save p-karanthaker/d9250a1b141e429a275a072d32ce37a1 to your computer and use it in GitHub Desktop.
Kills the process which is using a given port
#!/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