X-Git-Url: https://git.teslayout.com/public/public/public/?p=anna.git;a=blobdiff_plain;f=example%2Fdiameter%2Flauncher%2Fresources%2Fscripts%2Foperation_signal.sh;h=87e4ce5a586b9bac241d032ce36bb11b9cec4090;hp=1be6fa97ba6b7a7681b95560d06dc7295a285d93;hb=ea1b80f8fd0c1b806460f98f4e4cf433698857d9;hpb=18319deabbc8563fc112795968372e96263e6fe4 diff --git a/example/diameter/launcher/resources/scripts/operation_signal.sh b/example/diameter/launcher/resources/scripts/operation_signal.sh index 1be6fa9..87e4ce5 100755 --- a/example/diameter/launcher/resources/scripts/operation_signal.sh +++ b/example/diameter/launcher/resources/scripts/operation_signal.sh @@ -1,13 +1,91 @@ #!/bin/bash +############# +# VARIABLES # +############# +TIMEOUT__dflt=3 +SCR_BN=`basename $0` + ############# # FUNCTIONS # ############# -_exit () { +_exit() { echo -e "\n$1\n" exit 1 } +usage() { + echo + echo "Usage: $0 [-h|--help] [-t|--timeout ] [-f|--file ] [-p|--ping] [operation] " + echo + echo " -h|--help: this usage help." + echo " -t|--timeout : timeout for operation in seconds." + echo " Defaults to $TIMEOUT__dflt seconds if not provided." + echo " -f|--file : file with one operation per line (comments # allowed)." + echo " -p|--ping: Check the target process id." + echo " Returns 1 (dead) or 0 (alive)." + echo " operation: quoted operation string. Will be ignored if file" + echo " option is present. It is a positional argument" + echo " (the last one) when present." + echo + echo " For example:" + echo " $0 help" + echo " $0 \"test|ttps|50\"" + echo " $0 --file myOperationsList.txt" + echo " $0 --timeout 10 --file ./bigList.txt" + _exit +} + +parse_arguments() { + is_file= + timeout=$TIMEOUT__dflt + file= + operation= + ping= + + while [ $# -gt 0 ]; do + case "$1" in + -h|--help) + usage + ;; + + -t|--timeout) + timeout=$2 + [ -z "$timeout" ] && _exit "Missing timeout value" + shift + ;; + + -f|--file) + is_file=yes + file="$2" + [ -z "$file" ] && _exit "Missing file" + [ ! -f "$file" ] && _exit "Can't found provided file '$file'." + shift + ;; + + -p|--ping) + ping=yes + ;; + + *) + first=$(echo $1 | cut -c1) + [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options." + operation="$@" + break + ;; + esac + shift + done + + [ -z "$is_file" -a -z "$operation" -a -z "$ping" ] && _exit "Missing operation or file with operations" +} + +# $1: pid to check +check_pid() { + kill -0 $1 2>/dev/null + return $? +} + ############# # EXECUTION # ############# @@ -16,24 +94,48 @@ cd `dirname $0` [ ! -f .pid ] && _exit "Can't found '`pwd`/.pid'.\nTry to pgrep your process name and dump pid to that file." PID=`cat .pid` -# Send operation: -[ "$1" = "" ] && _exit "Usage: $0 [-f] ; i.e.: $0 help, $0 -f myOperationsList.txt" -FILE= -[ "$1" = "-f" ] && FILE=$2 +# Arguments: +[ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage +parse_arguments $@ + +# Check pid: +check_pid $PID +res=$? +[ -n "$ping" ] && exit $res +[ $res -ne 0 ] && _exit "Operation error: missing process with pid $PID" -if [ -z "$FILE" ] +# Send operation: +if [ -n "$is_file" ] then - echo $1 > sigusr2.in + cp $file sigusr2.in else - [ ! -f "$FILE" ] && _exit "Can't found provided file '$FILE'." - grep -v "^#" $FILE | sed '/^[ \t]*$/d' > sigusr2.in + echo $operation > sigusr2.in fi -0> sigusr2.out -kill -0 $PID 2>/dev/null -[ $? -ne 0 ] && _exit "Operation error: missing process with pid $PID" +0>sigusr2.out +check_pid $PID kill -s SIGUSR2 $PID # Detect EOF and print all except that last line: -while [ -z "$(tail -1 sigusr2.out | grep ^EOF)" ]; do sleep 0.1; done -head --lines=-1 `pwd`/sigusr2.out +count=$((10*timeout)) +expired=yes +while [ $count -gt 0 ] +do + sleep 0.1 + count=$((count-1)) + if tail -1 sigusr2.out | grep "^EOF" >/dev/null; then + expired= + break; + fi +done + +if [ -z "$expired" ] +then + head --lines=-1 sigusr2.out +else + _exit "Operation error: timeout expired ($timeout seconds)" +fi + +exception=$(grep "^Operation processed with exception: " sigusr2.out) +[ $? -eq 0 ] && _exit "(detected 'exception' within operation output: see 'launcher.trace')" +exit 0