-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecutable_wtfport
More file actions
62 lines (54 loc) · 1.5 KB
/
Copy pathexecutable_wtfport
File metadata and controls
62 lines (54 loc) · 1.5 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
# List out the pid for the process that is currently listening on the provided port
# Usage: wtfport find <port>
# Usage: wtfport kill <port>
# Example: wtfport find 3000
# Example: wtfport kill 3000
find_port() {
local port=$1
line="$(lsof -i -P -n | grep LISTEN | grep ":$port")"
pid=$(echo "$line" | awk '{print $2}')
pid_name=$(echo "$line" | awk '{print $1}')
# If there's nothing running, exit
if [[ -z "$pid" ]]; then
echo >&2 -e "No process found listening on port $port"
exit 0
fi
# output the process name to stderr so it won't be piped along
echo >&2 -e "Process \"$pid_name\" is listening on port $port"
# print the process id. It can be piped, for example to pbcopy
echo -e "$pid"
}
kill_port() {
local port=$1
line="$(lsof -i -P -n | grep LISTEN | grep ":$port")"
pid=$(echo "$line" | awk '{print $2}')
pid_name=$(echo "$line" | awk '{print $1}')
# If there's nothing running, exit
if [[ -z "$pid" ]]; then
echo >&2 -e "No process found listening on port $port"
exit 0
fi
kill -9 "$pid"
echo "Killed process \"$pid_name\" listening on port $port"
}
case "$1" in
find | f)
if [ -z "$2" ]; then
echo "Usage: wtfport find <port>"
exit 1
fi
find_port "$2"
;;
kill | k)
if [ -z "$2" ]; then
echo "Usage: wtfport kill <port>"
exit 1
fi
kill_port "$2"
;;
*)
echo "Usage: wtfport <find|kill> <port>"
exit 1
;;
esac