Skip to content
Andrew Mason @andrewmcodes

Kill Process Running on a Specific Port

Published
Reading time
1 min read
Tags
#unix

I often have to kill processes that weren’t stopped correctly on different ports and can never remember the command.

Snippet#

kill -9 $(lsof -ti tcp:4000)

Usage#

  1. Replace 4000 in the script above with the port you want to kill the process on.
  2. Run the script in your terminal.

Extending#

We can extract this into a function to make it easier to use.

terminate() {
  local port=$1
  local pid=$(lsof -ti tcp:$port)
  if [ -n "$pid" ]; then
    kill $pid
    echo "Killed process $pid on port $port"
  else
    echo "No process found on port $port"
  fi
}

Then we can use it like this:

terminate 4000
syndicated to rss

webmentions

No responses yet — be the first.

Written a response? Send a webmention.