srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | usage() { |
| 18 | cat <<EOF >&2 |
| 19 | |
| 20 | $0 - kill process listening on a port, wait for it to terminate |
| 21 | |
| 22 | Call syntax: |
| 23 | |
| 24 | $0 -p <port> [OPTS] |
| 25 | |
| 26 | OPTIONS: |
| 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 | |
| 32 | EOF |
| 33 | } |
| 34 | |
| 35 | PROGNAME="$0" |
| 36 | SHORTOPTS="hp:t:" |
| 37 | |
| 38 | ARGS=$(getopt $SHORTOPTS "$@" ) |
| 39 | |
| 40 | if [ $? -gt 0 ]; then |
| 41 | echo "Error getting options" >&2 |
| 42 | exit 2 |
| 43 | fi |
| 44 | |
| 45 | timeout=15 |
| 46 | |
| 47 | eval set -- "$ARGS" |
| 48 | |
| 49 | while 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 |
| 76 | done |
| 77 | |
| 78 | if [ ! "$port" ]; then |
| 79 | usage |
| 80 | exit 1 |
| 81 | fi |
| 82 | |
| 83 | for((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 |
| 89 | done |
| 90 | |
| 91 | echo "Error: Program on port $pid failed to exit" >&2 |
| 92 | exit 10 |