#!/bin/sh CHECK_PERIOD=1 TOTAL_PERIOD=60 LOG_ID="ubios-udapi-server-wait-init" # @brief Logs error. # # @param 1 - The error message. function log_error() { logger -s -t ${LOG_ID} -p 3 "${@}" } # @brief Logs info message. # # @param 1 - The info message. function log_info() { logger -t ${LOG_ID} "${@}" } # @brief Measures how long it takes to execute the passed command. # The exit status is that of command. # @param 1 - The command to execute. # @return The command execution time in traditional format. # @example # Command exited with non-zero status 255 # real 0.09 # user 0.06 # sys 0.03 function measure_cmd_time() { time -f '%e' -p "${@}" 2>&1 } # @brief Parses real time from traditional format. # @param 1 - The measured time in the traditional format. # @return The real time. function parse_real_time() { echo "${@}" | grep real | awk '{ print $2 }' } # @brief Waits until UUS is up and running function start() { printf "Waiting until ubios-udapi-server is up and running: " local ec=1 for i in $(seq ${CHECK_PERIOD} ${CHECK_PERIOD} ${TOTAL_PERIOD}); do local trad_time="" local real_time=0 local sleep_period=${CHECK_PERIOD} # issue UDAPI request; UUS is up and running if it succeeds trad_time=$(measure_cmd_time /bin/sh -c "/usr/bin/ubios-udapi-client -t ${CHECK_PERIOD} GET -r /server > /dev/null 2>&1") ec=$? [ "${ec}" -eq 0 ] && break # introduce throttling; ubios-udapi-client can respond immediately if the server socket doesn't exist real_time=$(parse_real_time "${trad_time}") sleep_period=$(echo "${sleep_period} - ${real_time}" | bc) sleep_period=$([ $(echo "${sleep_period} < 0" | bc) -eq 1 ] && echo 0 || echo ${sleep_period}) printf "." sleep ${sleep_period} done if [ "${ec}" -eq 0 ]; then echo "OK" log_info "ubios-udapi-server is up and running" else echo "FAIL" log_error "ubios-udapi-server hasn't started in the period of ${TOTAL_PERIOD} seconds" fi } case "$1" in start) start ;; stop) ;; restart|reload) start ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 esac exit $?