-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillport
More file actions
executable file
·39 lines (32 loc) · 818 Bytes
/
killport
File metadata and controls
executable file
·39 lines (32 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
if [ "$#" -lt 1 ]; then
echo "Usage: $0 PORT_NUMBER [PORT_NUMBER ...]"
exit 1
fi
for ARG in "$@"
do
if [[ "$ARG" =~ - ]]; then
START_PORT=$(echo "$ARG" | cut -d'-' -f1)
END_PORT=$(echo "$ARG" | cut -d'-' -f2)
for ((PORT=START_PORT; PORT<=END_PORT; PORT++)); do
PID=$(lsof -t -i:$PORT -sTCP:LISTEN)
if [ -z "$PID" ]; then
echo "No process found listening on port $PORT"
else
echo "Killing process $PID listening on port $PORT"
kill -9 $PID
fi
done
else
PORT=$ARG
PID=$(lsof -t -i:$PORT -sTCP:LISTEN)
if [ -z "$PID" ]; then
echo "No process found listening on port $PORT"
else
echo "Killing process $PID listening on port $PORT"
kill -9 $PID
fi
fi
done
echo "Processes killed done"
exit 0