cd /news/developer-tools/find-and-kill-the-process-holding-a-… · home › topics › developer-tools › article
[ARTICLE · art-172975] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

Find and kill the process holding a port

A developer built cmdbook, a tool that turns saved shell scripts into GUI forms by marking variable parts with placeholders such as {{prompt:port?3000}}. The accompanying example script finds processes listening on a port, shows the PID, owner, command, working directory and parent chain, and asks for confirmation before killing them, with a fallback to sudo kill and a force-kill prompt if the port stays busy.

key takeaway

Wrapping a saved shell script in a GUI only requires marking the changing values as placeholders, so the port-kill script prompts for a port (defaulting to 3000) and confirms before killing the PID.

by read3 min views6 publishedNov 20, 2026

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.

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}}

?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.

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

PORT={{prompt:port?3000}}

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."
── more in #developer-tools 4 stories · sorted by recency
── more on @cmdbook 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/find-and-kill-the-pr…] indexed:0 read:3min 2026-11-20 · —