DEV Community

Yingtse
Yingtse

Posted on

Find and kill the process holding a port

Port 3000 is taken, and you want to kill whatever is holding it.

How do you do it?

You ask an AI for a script, paste it into your terminal, and run it.

A week later, port 3000 is taken again. Asking the AI again feels slow, so this time you save the script somewhere: a Markdown note, or a kill-port.sh file.

Then it's port 8080. First you have to find where you saved it. Then you edit the port and run it.
Or you finally make the port an argument, or add a read -p "Port: ", so you don't have to edit it next time.

That's a lot of work for one small job, and you'll still have to find the file again.

With cmdbook, the script has one place to live, and it gets a GUI for free.

a form with a Port field

Here's how

1. Save the script once. Create a command in cmdbook and paste the script.

2. Mark what changes. Replace the port with a placeholder:

PORT={{prompt:port?3000}}
Enter fullscreen mode Exit fullscreen mode

?3000 is the default.

3. Click Run. cmdbook shows a form with a Port field, already filled in with 3000. Type another port, or just press Enter.

4. Decide in the output. The script shows what holds the port, then asks before killing it:

PID 37920  user apple, running 00:01
  command: python3 -m http.server 3000
  folder:  /Users/apple/code/my-app
  parent:  37918 npm run dev

Stop 37920 with 'kill'? [y/N] y
Port 3000 is free.
Enter fullscreen mode Exit fullscreen mode

You type y right in the output panel, as you would in a terminal.

Next time, your last port is already filled in. No searching, no editing, no asking again.

That's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.

Try cmdbook here: printf.app/cmdbook

The full script (macOS)
PORT={{prompt:port?3000}}

# Who listens on the port. lsof only sees your own processes;
# netstat -anv (macOS) also shows other users' listeners, with the PID.
PIDS=$(lsof -nP -tiTCP:"$PORT" -sTCP:LISTEN | sort -u)
if [ -z "$PIDS" ]; then
  PIDS=$(netstat -anv -p tcp 2>/dev/null | awk -v p="$PORT" '
    $6 == "LISTEN" && $4 ~ "[.:]" p "$" {
      for (i = 7; i <= NF; i++) if ($i ~ /^[^:]+:[0-9]+$/) { split($i, a, ":"); print a[2] }
    }' | sort -u)
fi
if [ -z "$PIDS" ]; then
  echo "Nothing is listening on port $PORT."
  exit 1
fi

KILL="kill"
for PID in $PIDS; do
  OWNER=$(ps -o user= -p "$PID" | tr -d ' ')
  [ "$OWNER" != "$(id -un)" ] && KILL="sudo kill"
  echo "PID $PID  user $OWNER, running $(ps -o etime= -p "$PID" | tr -d ' ')"
  echo "  command: $(ps -o command= -p "$PID" | cut -c1-120)"
  CWD=$(lsof -a -p "$PID" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p')
  echo "  folder:  ${CWD:-(owned by another user: needs sudo to see)}"
  P=$(ps -o ppid= -p "$PID" | tr -d ' ')
  while [ -n "$P" ] && [ "$P" -gt 1 ]; do
    echo "  parent:  $P $(ps -o command= -p "$P" | cut -c1-100)"
    P=$(ps -o ppid= -p "$P" | tr -d ' ')
  done
done

echo
printf "Stop %s with '%s'? [y/N] " "$(echo $PIDS)" "$KILL"
read -r ANSWER
case "$ANSWER" in y|Y|yes) ;; *) echo "Left running."; exit 0 ;; esac

$KILL $PIDS || exit 1
for i in 1 2 3 4 5; do
  sleep 1
  NOW=$(lsof -nP -tiTCP:"$PORT" -sTCP:LISTEN | sort -u)
  if [ -z "$NOW" ] && ! netstat -an -p tcp | grep -q "[.:]$PORT .*LISTEN"; then
    echo "Port $PORT is free."; exit 0
  fi
  if [ -n "$NOW" ] && [ "$NOW" != "$PIDS" ]; then
    echo "Port $PORT was taken again by PID $(echo $NOW): something restarted it (a file watcher, its parent, or launchd)."
    exit 1
  fi
done

printf "Still listening after 5 s. Force it (%s -9)? [y/N] " "$KILL"
read -r ANSWER
case "$ANSWER" in y|Y|yes) $KILL -9 $PIDS; sleep 1 ;; *) echo "Left running."; exit 1 ;; esac
if netstat -an -p tcp | grep -q "[.:]$PORT .*LISTEN"; then echo "Port $PORT is still busy."; exit 1; fi
echo "Port $PORT is free."

Top comments (0)