{"slug": "find-and-kill-the-process-holding-a-port", "title": "Find and kill the process holding a port", "summary": "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.", "body_md": "Port 3000 is taken, and you want to kill whatever is holding it.\n\nHow do you do it?\n\nYou ask an AI for a script, paste it into your terminal, and run it.\n\nA 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.\n\nThen it's port 8080. First you have to find where you saved it. Then you edit the port and run it.\n\nOr you finally make the port an argument, or add a `read -p \"Port: \"`, so you don't have to edit it next time.\n\nThat's a lot of work for one small job, and you'll still have to find the file again.\n\nWith [cmdbook](https://printf.app/cmdbook), the script has one place to live, and it gets a GUI for free.\n\n**1. Save the script once.** Create a command in cmdbook and paste the script.\n\n**2. Mark what changes.** Replace the port with a placeholder:\n\n```\nPORT={{prompt:port?3000}}\n```\n\n`?3000` is the default.\n\n**3. Click Run.** cmdbook shows a form with a Port field, already filled in with 3000. Type another port, or just press Enter.\n\n**4. Decide in the output.** The script shows what holds the port, then asks before killing it:\n\n```\nPID 37920  user apple, running 00:01\n  command: python3 -m http.server 3000\n  folder:  /Users/apple/code/my-app\n  parent:  37918 npm run dev\n\nStop 37920 with 'kill'? [y/N] y\nPort 3000 is free.\n```\n\nYou type `y` right in the output panel, as you would in a terminal.\n\nNext time, your last port is already filled in. No searching, no editing, no asking again.\n\nThat's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.\n\nTry cmdbook here: [printf.app/cmdbook](https://printf.app/cmdbook)\n\n```\nPORT={{prompt:port?3000}}\n\n# Who listens on the port. lsof only sees your own processes;\n# netstat -anv (macOS) also shows other users' listeners, with the PID.\nPIDS=$(lsof -nP -tiTCP:\"$PORT\" -sTCP:LISTEN | sort -u)\nif [ -z \"$PIDS\" ]; then\n  PIDS=$(netstat -anv -p tcp 2>/dev/null | awk -v p=\"$PORT\" '\n    $6 == \"LISTEN\" && $4 ~ \"[.:]\" p \"$\" {\n      for (i = 7; i <= NF; i++) if ($i ~ /^[^:]+:[0-9]+$/) { split($i, a, \":\"); print a[2] }\n    }' | sort -u)\nfi\nif [ -z \"$PIDS\" ]; then\n  echo \"Nothing is listening on port $PORT.\"\n  exit 1\nfi\n\nKILL=\"kill\"\nfor PID in $PIDS; do\n  OWNER=$(ps -o user= -p \"$PID\" | tr -d ' ')\n  [ \"$OWNER\" != \"$(id -un)\" ] && KILL=\"sudo kill\"\n  echo \"PID $PID  user $OWNER, running $(ps -o etime= -p \"$PID\" | tr -d ' ')\"\n  echo \"  command: $(ps -o command= -p \"$PID\" | cut -c1-120)\"\n  CWD=$(lsof -a -p \"$PID\" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p')\n  echo \"  folder:  ${CWD:-(owned by another user: needs sudo to see)}\"\n  P=$(ps -o ppid= -p \"$PID\" | tr -d ' ')\n  while [ -n \"$P\" ] && [ \"$P\" -gt 1 ]; do\n    echo \"  parent:  $P $(ps -o command= -p \"$P\" | cut -c1-100)\"\n    P=$(ps -o ppid= -p \"$P\" | tr -d ' ')\n  done\ndone\n\necho\nprintf \"Stop %s with '%s'? [y/N] \" \"$(echo $PIDS)\" \"$KILL\"\nread -r ANSWER\ncase \"$ANSWER\" in y|Y|yes) ;; *) echo \"Left running.\"; exit 0 ;; esac\n\n$KILL $PIDS || exit 1\nfor i in 1 2 3 4 5; do\n  sleep 1\n  NOW=$(lsof -nP -tiTCP:\"$PORT\" -sTCP:LISTEN | sort -u)\n  if [ -z \"$NOW\" ] && ! netstat -an -p tcp | grep -q \"[.:]$PORT .*LISTEN\"; then\n    echo \"Port $PORT is free.\"; exit 0\n  fi\n  if [ -n \"$NOW\" ] && [ \"$NOW\" != \"$PIDS\" ]; then\n    echo \"Port $PORT was taken again by PID $(echo $NOW): something restarted it (a file watcher, its parent, or launchd).\"\n    exit 1\n  fi\ndone\n\nprintf \"Still listening after 5 s. Force it (%s -9)? [y/N] \" \"$KILL\"\nread -r ANSWER\ncase \"$ANSWER\" in y|Y|yes) $KILL -9 $PIDS; sleep 1 ;; *) echo \"Left running.\"; exit 1 ;; esac\nif netstat -an -p tcp | grep -q \"[.:]$PORT .*LISTEN\"; then echo \"Port $PORT is still busy.\"; exit 1; fi\necho \"Port $PORT is free.\"\n```\n\n", "url": "https://wpnews.pro/news/find-and-kill-the-process-holding-a-port", "canonical_source": "https://dev.to/yingtse/find-and-kill-the-process-holding-a-port-7e9", "published_at": "2026-11-20 03:06:54+00:00", "updated_at": "2026-11-20 03:12:58.741933+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["cmdbook", "printf.app"], "also_reported_by": [], "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.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/find-and-kill-the-process-holding-a-port", "markdown": "https://wpnews.pro/news/find-and-kill-the-process-holding-a-port.md", "text": "https://wpnews.pro/news/find-and-kill-the-process-holding-a-port.txt", "jsonld": "https://wpnews.pro/news/find-and-kill-the-process-holding-a-port.jsonld"}}