3 # ============LICENSE_START=======================================================
4 # Copyright (C) 2019 Nordix Foundation.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
22 # @author Waqas Ikram (waqas.ikram@est.tech)
24 SCRIPT_NAME=$(basename $0)
26 # Process the arguments passed to the script
34 Mandatory argument. time out value in seconds (must be number)
37 Mandatory argument. Host name or IP
40 Mandatory argument. Port of the host
43 Optional argument. Display this usage.
51 date +"%Y-%m-%d %H:%M:%S"
54 # Called when script is executed with invalid arguments
57 echo "Missing or invalid option(s):"
59 echo "Try --help for more information"
66 LONG_ARGS="help,timeout:,host:,port:"
68 args=$(getopt -o $SHORT_ARGS -l $LONG_ARGS -n "$0" -- "$@" 2>&1 )
69 [[ $? -ne 0 ]] && invalid_arguments $( echo " $args"| head -1 )
70 [[ $# -eq 0 ]] && invalid_arguments "No options provided"
94 echo BAD ARGUMENTS # perhaps error
100 if ! [[ $TIME_OUT =~ $regex ]] ; then
101 echo "$SCRIPT_NAME $(current_timestamp): error: TIME_OUT must be number $TIME_OUT" >&2; exit 1
104 if [ -z "$HOST" ]; then
105 echo "$SCRIPT_NAME $(current_timestamp): error: HOST must not be empty! $HOST" >&2; exit 1
108 if ! [[ $PORT =~ $regex ]]; then
109 echo "$SCRIPT_NAME $(current_timestamp): error: PORT must be number! $PORT" >&2; exit 1
113 START_TIME_IN_SECONDS=`date +%s`
114 TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT));
116 while [ `date +%s` -lt "$TIME_OUT_END_TIME_IN_SECONDS" ]; do
117 echo "$(current_timestamp): Waiting for $HOST:$PORT to startup ..."
119 nc -z "$HOST" "$PORT" > /dev/null 2>&1
121 if [ $result -eq 0 ] ; then
122 echo "$SCRIPT_NAME $(current_timestamp): $HOST:$PORT is up and running"
125 echo "$SCRIPT_NAME $(current_timestamp): Sleeping for ${SLEEP_TIME} seconds"
129 if [ $result -ne 0 ]; then
130 echo "$SCRIPT_NAME $(current_timestamp): Time out: could not get any response from $HOST:$PORT . . ."
134 echo "$SCRIPT_NAME $(current_timestamp): finished successfully"