blob: 0c2dcdca3a7332af985e216cd9fe91f3aba14b50 [file] [log] [blame]
srikanth116e6e82014-08-19 07:22:37 -07001#!/bin/bash
2#
3# Copyright (c) 2013 Big Switch Networks, Inc.
4#
5# Licensed under the Eclipse Public License, Version 1.0 (the
6# "License"); you may not use this file except in compliance with the
7# License. You may obtain a copy of the License at
8#
9# http://www.eclipse.org/legal/epl-v10.html
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied. See the License for the specific language governing
15# permissions and limitations under the License.
16
17usage() {
18 cat <<EOF >&2
19
20$0 - kill process listening on a port, wait for it to terminate
21
22Call syntax:
23
24 $0 -p <port> [OPTS]
25
26OPTIONS:
27 -p <port> wait for port <port> to listen on
28 -t <time> wait <t> seconds for port to show up
29
30 -h this help
31
32EOF
33}
34
35PROGNAME="$0"
36SHORTOPTS="hp:t:"
37
38ARGS=$(getopt $SHORTOPTS "$@" )
39
40if [ $? -gt 0 ]; then
41 echo "Error getting options" >&2
42 exit 2
43fi
44
45timeout=15
46
47eval set -- "$ARGS"
48
49while true; do
50 case $1 in
51 -h)
52 usage
53 exit 0
54 ;;
55 -p)
56 port="$2"
57 shift
58 ;;
59 -t)
60 timeout="$2"
61 shift
62 ;;
63 --)
64 shift
65 break
66 ;;
67 -*)
68 echo "$0: error - unrecognized option $1" 1>&2
69 exit 1
70 ;;
71 *)
72 break
73 ;;
74 esac
75 shift
76done
77
78if [ ! "$port" ]; then
79 usage
80 exit 1
81fi
82
83for((i=0; i < 15; i++ )); do
84 if ! pid=$(lsof -t -iTCP:$port -sTCP:LISTEN); then
85 exit 0
86 fi
87 kill ${pid}
88 sleep 1
89done
90
91echo "Error: Program on port $pid failed to exit" >&2
92exit 10