-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill_port.sh
executable file
·62 lines (39 loc) · 1.64 KB
/
kill_port.sh
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
# **************************************************************************** #
# #
# ::: :::::::: #
# close_port.sh :+: :+: :+: #
# +:+ +:+ +:+ #
# By: amarquez <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2017/07/25 18:06:36 by amarquez #+# #+# #
# Updated: 2017/07/25 18:50:15 by amarquez ### ########.fr #
# #
# **************************************************************************** #
## target name
target='server'
## ansi colors
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'
## ask for port number
printf "${YELLOW}Enter port number to be close: ${NC}";
## assign port number
read -p "" port
## check if valid port number
if [ "$port" -eq "$port" ] 2>/dev/null; then
# get output from lsof ( including PID )
printf "${YELLOW}Looking for port :[ ${port} ] PID${NC}\n"
pid="$(lsof -i :$port | grep $target | cut -d ' ' -f 3)"
# check if pid is a num
if [ "$pid" -eq "$pid" ] 2>/dev/null; then
## kill pid
printf "${GREEN}PID found, killing process: ${NC}"
kill $pid
else
printf "${RED}Unable to find PID, bye${NC}\n"
fi
else
# not a valid port number
printf "${RED}Not a valid port number, bye\n${NC}";
fi