Remove operation help.
[anna.git] / example / diameter / launcher / resources / scripts / operation_signal.sh
1 #!/bin/bash
2
3 #############
4 # VARIABLES #
5 #############
6 TIMEOUT__dflt=3
7 SCR_BN=`basename $0`
8
9 #############
10 # FUNCTIONS #
11 #############
12 _exit() {
13   echo -e "\n$1\n"
14   exit 1
15 }
16
17 usage() {
18   echo
19   echo "Usage: $0 [-h|--help] [-t|--timeout <value>] [-f|--file <file>] [-p|--ping] [operation] "
20   echo
21   echo "       -h|--help:             this usage help."
22   echo "       -t|--timeout <value>:  timeout for operation in seconds."
23   echo "                              Defaults to $TIMEOUT__dflt seconds if not provided."
24   echo "       -f|--file <file>:      file with one operation per line (comments # allowed)."
25   echo "       -p|--ping:             Check the target process id."
26   echo "                              Returns 1 (dead) or 0 (alive)."
27   echo "       operation:             quoted operation string. Will be ignored if file"
28   echo "                              option is present. It is a positional argument"
29   echo "                              (the last one) when present."
30   echo
31   echo "       For example:"
32   echo "          $0 help"
33   echo "          $0 \"test|ttps|50\""
34   echo "          $0 --file myOperationsList.txt"
35   echo "          $0 --timeout 10 --file ./bigList.txt"
36   _exit
37 }
38
39 parse_arguments() {
40   is_file=
41   timeout=$TIMEOUT__dflt
42   file=
43   operation=
44   ping=
45
46   while [ $# -gt 0 ]; do
47     case "$1" in
48       -h|--help)
49         usage
50       ;;
51
52       -t|--timeout)
53         timeout=$2
54         [ -z "$timeout" ] && _exit "Missing timeout value"
55         shift
56       ;;
57
58       -f|--file)
59         is_file=yes
60         file="$2"
61         [ -z "$file" ] && _exit "Missing file"
62         [ ! -f "$file" ] && _exit "Can't found provided file '$file'."
63         shift
64       ;;
65
66       -p|--ping)
67         ping=yes
68       ;;
69
70       *)
71         first=$(echo $1 | cut -c1)
72         [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options."
73         operation="$@"
74         break
75       ;;
76     esac
77     shift
78   done
79
80   [ -z "$is_file" -a -z "$operation" -a -z "$ping" ] && _exit "Missing operation or file with operations"
81 }
82
83 # $1: pid to check
84 check_pid() {
85   kill -0 $1 2>/dev/null
86   return $?
87 }
88
89 #############
90 # EXECUTION #
91 #############
92 cd `dirname $0`
93 # Get the PID:
94 [ ! -f .pid ] && _exit "Can't found '`pwd`/.pid'.\nTry to pgrep your process name and dump pid to that file."
95 PID=`cat .pid`
96
97 # Arguments:
98 [ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage
99 parse_arguments $@
100
101 # Check pid:
102 check_pid $PID
103 res=$?
104 [ -n "$ping" ] && exit $res
105 [ $res -ne 0 ] && _exit "Operation error: missing process with pid $PID"
106
107 # Send operation:
108 if [ -n "$is_file" ]
109 then
110   cp $file sigusr2.in
111 else
112   echo $operation > sigusr2.in
113 fi
114 0>sigusr2.out
115 check_pid $PID
116 kill -s SIGUSR2 $PID
117
118 # Detect EOF and print all except that last line:
119 count=$((10*timeout))
120 expired=yes
121 while [ $count -gt 0 ]
122 do
123   sleep 0.1
124   count=$((count-1))
125   if tail -1 sigusr2.out | grep "^EOF" >/dev/null; then
126     expired=
127     break;
128   fi
129 done
130
131 if [ -z "$expired" ]
132 then
133   head --lines=-1 sigusr2.out
134 else
135   _exit "Operation error: timeout expired ($timeout seconds)"
136 fi
137
138 exception=$(grep "^Operation processed with exception: " sigusr2.out)
139 [ $? -eq 0 ] && _exit "(detected 'exception' within operation output: see 'launcher.trace')"
140 exit 0
141