blob: 15f4e4a722cb1382b780d040de0cd707dda2f403 [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 - start process in the background, wait 15 secs for port to come up
21
22Call syntax:
23
24 $0 -p <port> [OPTS] <cmd> [arg1] [arg2...]
25
26OPTIONS:
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
33EOF
34}
35
36
37PROGNAME="$0"
38SHORTOPTS="hp:l:t:"
39
40ARGS=$(getopt $SHORTOPTS "$@" )
41
42if [ $? -gt 0 ]; then
43 echo "Error parsing options" >&2
44 exit 1
45fi
46timeout=15
47
48eval set -- "$ARGS"
49
50while 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
81done
82
83if [ ! "$port" -o ! "$*" ]; then
84 usage
85 exit 1
86fi
87
88if [ ! "$log" ]; then
89 if [ "$VIRTUALENV" ]; then
90 logdir="$VIRTUALENV/log"
91 else
92 logdir="/tmp"
93 fi
94 log="$logdir/$(basename $1).log"
95fi
96
97if pid=$(lsof -t -iTCP:$port -sTCP:LISTEN); then
98 echo "$* already running on port $port" >&1
99 exit 0
100fi
101
102echo "Launching $* (log: $log)" >&2
103
104nohup "$@" >$log 2>&1 &
105if [ $? -gt 0 ]; then
106 echo "Error: Launch of $@ failed with exit code $?" >&2
107 exit 11
108fi
109
110echo -n "Waiting for port $port..." >&2
111for((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 "."
118done
119
120echo " FAIL: Launched program $@ failed to bring up port within $timeout seconds" >&2
121exit 10