-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillport
More file actions
executable file
·55 lines (46 loc) · 1.25 KB
/
killport
File metadata and controls
executable file
·55 lines (46 loc) · 1.25 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
#!/usr/bin/env bash
####################################################
# Kills the process running on the specified port #
####################################################
sendSigKill=false
renderHelp() {
echo "Kills a service running on the specified port."
echo "Usage: killport [OPTIONS] PORT"
echo "Options:"
echo " -f Forcefully kill the process by issuing a SIGKILL, rather than SIGTERM."
exit 0
}
while getopts ":hf" opt; do
case ${opt} in
h )
renderHelp
;;
f )
sendSigKill=true
;;
esac
done
shift $((OPTIND -1))
port=$1
if [[ ${#port} == 0 ]]; then
renderHelp
fi
# Get everything running on this port
lsofcmd="lsof -i :$port"
# echo the command, and then iterate through each line of the output
counter=0
$(echo $lsofcmd) | while read -r line; do
counter=$((counter+1)) # We want to skip the first line, as the first line is the column headers, from lsof
if [[ $counter > 1 ]]; then
procname=$(echo $line | awk '{print $1}')
pid=$(echo $line | awk '{print $2}')
killFlags=''
logMessage="Killing $procname with PID: $pid"
if [ "$sendSigKill" = true ]; then
logMessage="Force-$logMessage"
killFlags="-9"
fi
echo "$logMessage"
kill "$killFlags" $pid;
fi
done