Skip to content

Commit de5cd25

Browse files
jrvb-rlclaude
andcommitted
Add /proc fallback for finding processes on a port
When lsof, ss, and fuser are all unavailable, parse /proc/net/tcp to find socket inodes listening on the target port, then scan /proc/*/fd/ to map inodes back to pids. Always available on Linux. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a473e2f commit de5cd25

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

scripts/test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,37 @@ function steady_is_running() {
1313
curl --silent "http://127.0.0.1:4010/_x-steady/health" >/dev/null 2>&1
1414
}
1515

16+
find_pids_on_port_via_proc() {
17+
# Parse /proc/net/tcp{,6} to find socket inodes listening on the port,
18+
# then scan /proc/*/fd/ to find which pids own those sockets.
19+
local port_hex
20+
port_hex=$(printf '%04X' "$1")
21+
local inodes
22+
inodes=$(awk -v ph="$port_hex" '$2 ~ ":"ph"$" && $4 == "0A" {print $10}' /proc/net/tcp /proc/net/tcp6 2>/dev/null | sort -u)
23+
[ -z "$inodes" ] && return
24+
local pids=""
25+
for inode in $inodes; do
26+
for fd in /proc/[0-9]*/fd/*; do
27+
if [ "$(readlink "$fd" 2>/dev/null)" = "socket:[$inode]" ]; then
28+
local pid="${fd#/proc/}"
29+
pid="${pid%%/fd/*}"
30+
pids="$pids $pid"
31+
break
32+
fi
33+
done
34+
done
35+
echo "$pids" | xargs
36+
}
37+
1638
kill_server_on_port() {
1739
if command -v lsof >/dev/null 2>&1; then
1840
pids=$(lsof -t -i tcp:"$1" || echo "")
1941
elif command -v ss >/dev/null 2>&1; then
2042
pids=$(ss -tlnp "sport = :$1" 2>/dev/null | grep -oP 'pid=\K[0-9]+' || echo "")
2143
elif command -v fuser >/dev/null 2>&1; then
2244
pids=$(fuser "$1/tcp" 2>/dev/null || echo "")
45+
elif [ -d /proc/net ]; then
46+
pids=$(find_pids_on_port_via_proc "$1")
2347
else
2448
echo "Warning: no tool available to find processes on port $1"
2549
return

0 commit comments

Comments
 (0)