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 - start process in the background, wait 15 secs for port to come up |
| 21 | |
| 22 | Call syntax: |
| 23 | |
| 24 | $0 -p <port> [OPTS] <cmd> [arg1] [arg2...] |
| 25 | |
| 26 | OPTIONS: |
| 27 | -p <port> wait for port <port> to listen on |
| 28 | -l <logfile> logfile to use for nohup output |
| 29 | -t <time> wait <time> seconds for port to show up |
| 30 | |
| 31 | -h this help |
| 32 | |
| 33 | EOF |
| 34 | } |
| 35 | |
| 36 | |
| 37 | PROGNAME="$0" |
| 38 | SHORTOPTS="hp:l:t:" |
| 39 | |
| 40 | ARGS=$(getopt $SHORTOPTS "$@" ) |
| 41 | |
| 42 | if [ $? -gt 0 ]; then |
| 43 | echo "Error parsing options" >&2 |
| 44 | exit 1 |
| 45 | fi |
| 46 | timeout=15 |
| 47 | |
| 48 | eval set -- "$ARGS" |
| 49 | |
| 50 | while true; do |
| 51 | case $1 in |
| 52 | -h) |
| 53 | usage |
| 54 | exit 0 |
| 55 | ;; |
| 56 | -p) |
| 57 | port="$2" |
| 58 | shift |
| 59 | ;; |
| 60 | -l) |
| 61 | log="$2" |
| 62 | shift |
| 63 | ;; |
| 64 | -t) |
| 65 | timeout="$2" |
| 66 | shift |
| 67 | ;; |
| 68 | --) |
| 69 | shift |
| 70 | break |
| 71 | ;; |
| 72 | -*) |
| 73 | echo "$0: error - unrecognized option $1" 1>&2 |
| 74 | exit 1 |
| 75 | ;; |
| 76 | *) |
| 77 | break |
| 78 | ;; |
| 79 | esac |
| 80 | shift |
| 81 | done |
| 82 | |
| 83 | if [ ! "$port" -o ! "$*" ]; then |
| 84 | usage |
| 85 | exit 1 |
| 86 | fi |
| 87 | |
| 88 | if [ ! "$log" ]; then |
| 89 | if [ "$VIRTUALENV" ]; then |
| 90 | logdir="$VIRTUALENV/log" |
| 91 | else |
| 92 | logdir="/tmp" |
| 93 | fi |
| 94 | log="$logdir/$(basename $1).log" |
| 95 | fi |
| 96 | |
| 97 | if pid=$(lsof -t -iTCP:$port -sTCP:LISTEN); then |
| 98 | echo "$* already running on port $port" >&1 |
| 99 | exit 0 |
| 100 | fi |
| 101 | |
| 102 | echo "Launching $* (log: $log)" >&2 |
| 103 | |
| 104 | nohup "$@" >$log 2>&1 & |
| 105 | if [ $? -gt 0 ]; then |
| 106 | echo "Error: Launch of $@ failed with exit code $?" >&2 |
| 107 | exit 11 |
| 108 | fi |
| 109 | |
| 110 | echo -n "Waiting for port $port..." >&2 |
| 111 | for((i=0; i < $timeout; i++ )); do |
| 112 | if pid=$(lsof -t -iTCP:$port -sTCP:LISTEN); then |
| 113 | echo " OK" |
| 114 | exit 0 |
| 115 | fi |
| 116 | sleep 1 |
| 117 | echo -n "." |
| 118 | done |
| 119 | |
| 120 | echo " FAIL: Launched program $@ failed to bring up port within $timeout seconds" >&2 |
| 121 | exit 10 |