Skip to content

Instantly share code, notes, and snippets.

@noahlt
Created October 23, 2011 07:19
Show Gist options
  • Save noahlt/1306985 to your computer and use it in GitHub Desktop.
Save noahlt/1306985 to your computer and use it in GitHub Desktop.
How do I kill whatever process is listening to a particular port?
function killport {
if [ $1 == '-h' ] || [ -z $1 ]; then
echo '`killport <PORT>` finds the process listening to the specified port and kills it.'
else
process_line=`sudo lsof -i :$1 | tail -1`
if [ "$process_line" == "" ]; then
echo "no processes listening on $1"
else
process_name=`echo "$process_line" | awk '{print $1}'`
echo "killing $process_name"
sudo kill `echo "$process_line" | awk '{print $2}'`
fi
fi
}

Add the attached bash function to your .bashrc (or bash_profile), then source .bashrc (or bash_profile) and use it like this:

$ killport 8000

killport will tell you the name of the killed process, or it will let you know if it couldn't find any process with that port open.

["bash","kill","port"]
@jrnxf
Copy link

jrnxf commented Nov 7, 2019

this is great - thanks!

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