]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh.in
LP1204123 osrf_ctl.sh supports sending signals
[OpenSRF.git] / bin / osrf_ctl.sh.in
1 #!/bin/sh
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12  
13 # Strictness to avoid folly
14 set -e
15 set -u
16
17 prefix=@prefix@
18 exec_prefix=@exec_prefix@
19
20 OPT_ACTION=""
21 OPT_CONFIG=""
22 OPT_PID_DIR=""
23 OPT_SIGNAL=""
24 OPT_SERVICE=""
25 OSRF_HOSTNAME=""
26
27 # ---------------------------------------------------------------------------
28 # Make sure we're running as the correct user
29 # ---------------------------------------------------------------------------
30 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
31
32
33 usage() {
34         cat << EOF
35
36 usage: $0 [OPTION]... -c <c_config> -a <action>
37 usage: $0 -a signal -k <signal> -s <service>
38
39 Mandatory parameters:
40   -a    action to perform
41
42 Optional parameters:
43   -c    full path to C configuration file (opensrf_core.xml)
44   -d    store PID files in this directory
45   -l    accept 'localhost' as the fully-qualified domain name
46   -k    signal to send to process. Argument is passed directly to 'kill'.
47   -s    service name.  Used in conjunction with -k.
48
49
50 Actions include:
51     start_router
52     stop_router
53     restart_router
54     start_perl
55     stop_perl
56     restart_perl
57     start_python
58     stop_python
59     restart_python
60     start_c
61     stop_c
62     restart_c
63     start_osrf
64     stop_osrf
65     restart_osrf
66     stop_all
67     start_all
68     restart_all
69     smart_clear     - Clear all PID files that don't refer to a process 
70     clear_pid       - Clear all PID files
71     signal          - send signal specified by -k to service specified by -s
72     signal_all      - send signal specified by -k to all services
73
74
75 Examples:
76   $0 -a restart_all
77   $0 -l -c opensrf_core.xml -a restart_all
78   $0 -a signal -k opensrf.settings -s USR1
79   $0 -a signal_all -s HUP
80
81 EOF
82 }
83
84 # ---------------------------------------------------------------------------
85 # Load the command line options and set the global vars
86 # ---------------------------------------------------------------------------
87 while getopts  "a:d:c:s:k:lh" flag; do
88         case $flag in   
89                 "a")            OPT_ACTION="$OPTARG";;
90                 "c")            OPT_CONFIG="$OPTARG";;
91                 "d")            OPT_PID_DIR="$OPTARG";;
92                 "s")            OPT_SERVICE="$OPTARG";;
93                 "k")            OPT_SIGNAL="$OPTARG";;
94                 "l")            export OSRF_HOSTNAME="localhost";;
95                 "h"|*)  usage;;
96         esac;
97 done
98
99 OSRF_CONFIG="@bindir@/osrf_config"
100 [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config`
101
102 [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml;
103 if [ ! -r "$OPT_CONFIG" ]; then
104         echo "Please specify the location of the opensrf_core.xml file using the -c flag";
105         exit 1;
106 fi;
107 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run/opensrf;
108 [ -z "$OPT_ACTION" ] && usage;
109
110 PID_ROUTER="$OPT_PID_DIR/router.pid";
111 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
112 PID_OSRF_PYTHON="$OPT_PID_DIR/osrf_python.pid";
113 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
114
115 # apply some sanity checks to the signal handling arguments
116 if [ ${OPT_ACTION%_all} = "signal" ]; then
117     warning="";
118
119     [ -z "$OPT_SIGNAL" ] && \
120         warning="-a $OPT_ACTION requires a signal specified by -k";
121
122     [ $OPT_ACTION = "signal" -a -z "$OPT_SERVICE" ] && \
123         warning="$warning\n-a $OPT_ACTION requires a service specified by -s";
124
125     [ $OPT_ACTION = "signal_all" -a -n "$OPT_SERVICE" ] && \
126         warning="$warning\n-s $OPT_SERVICE cannot be used together with $OPT_ACTION";
127
128     if [ -n "$warning" ]; then
129         usage;
130         echo "------------"
131         echo -e $warning;
132         exit;
133     fi;
134
135     [ $OPT_ACTION = "signal_all" ] && OPT_SERVICE="*";
136 fi;
137
138 # ---------------------------------------------------------------------------
139 # Utility code for checking the PID files
140 # ---------------------------------------------------------------------------
141 do_action() {
142
143         action="$1"; 
144         pidfile="$2";
145         item="$3"; 
146
147         if [ $action = "start" ]; then
148
149                 if [ -e $pidfile ]; then
150                         pid=$(cat $pidfile);
151                         reported=$(ps ax | grep "$item" | grep -v grep | awk '{print $1}' | awk '{ printf "%s ", $0 }')
152                         
153                         if [ "$pid " = "$reported" ]; then
154                                 echo "$item already started : $pid";
155                                 return 0;
156                         else
157                                 echo "$item not started, but PID file exists, removing file and starting";
158                                 rm $pidfile;
159                                 return 0;
160                         fi;
161                 fi;
162                 echo "Starting $item";
163         fi;
164
165         if [ $action = "stop" ]; then
166
167                 if [ ! -e $pidfile ]; then
168                         echo "$item not running";
169                         return 0;
170                 fi;
171
172         while read pid; do
173             if [ -n "$pid" ]; then
174                 echo "Stopping $item process $pid..."
175                 kill -s INT $pid
176             fi;
177         done < $pidfile;
178                 rm -f $pidfile;
179
180         fi;
181
182         return 0;
183 }
184
185
186 # ---------------------------------------------------------------------------
187 # Start / Stop functions
188 # ---------------------------------------------------------------------------
189
190
191 start_router() {
192         do_action "start" $PID_ROUTER "OpenSRF Router";
193         opensrf_router $OPT_CONFIG routers
194     sleep 2; # give the router procs time to fork and appear in ps
195         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
196         echo $pid > $PID_ROUTER;
197         return 0;
198 }
199
200 stop_router() {
201         do_action "stop" $PID_ROUTER "OpenSRF Router";
202         return 0;
203 }
204
205 # The clean way to do this would be to invoke the "start_all" action
206 # on opensrf.py, but that seems to randomly fail for services. So we
207 # start each one separately from a bash script instead.
208 start_python() {
209     [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
210     echo "Starting OpenSRF Python";
211     
212     if [ -e $PID_OSRF_PYTHON ]; then  #If python is started already (or it thinks so).
213     cat << EOF
214 Python processes are either already running, or were not correctly shut down.
215 Now clearing any stale PID files and restarting Perl processes.
216 EOF
217     smart_clear;
218     fi;
219
220     OPT_LOCAL=""
221     [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l"
222     for service in `opensrf.py -a list_all $OPT_LOCAL`; do
223             opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -d -a start -s $service $OPT_LOCAL
224             [ "$?" = "0" ] && echo "Python Started" > $PID_OSRF_PYTHON;  #Dummy pid file, removed when a proper shutdown happens.
225     done
226     return 0;
227 }
228
229 stop_python() {
230     [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
231     echo "Stopping OpenSRF Python";
232     [ -e $PID_OSRF_PYTHON ] && rm $PID_OSRF_PYTHON;
233     OPT_LOCAL=""
234     [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l"
235     set +e # Ignore errors for NXDOMAIN
236     opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -a stop_all $OPT_LOCAL
237     set -e # Errors matter again
238     sleep 1;
239     return 0;
240 }
241
242 start_perl() {
243     echo "Starting OpenSRF Perl";
244     
245     if [ -e $PID_OSRF_PERL ]; then  #If perl is started already (or it thinks so)
246     cat << EOF
247 Perl processes are either already running, or were not correctly shut down.
248 Now clearing any stale PID files and restarting Perl processes.
249 EOF
250     smart_clear;
251     fi;
252     
253     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
254         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
255     [ "$?" = "0" ] && echo "Perl Started" > $PID_OSRF_PERL;  #Dummy pid file, removed when a proper shutdown happens.
256         return 0;
257 }
258
259 stop_perl() {
260     echo "Stopping OpenSRF Perl";
261     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
262     [ -e $PID_OSRF_PERL ] && rm $PID_OSRF_PERL;
263         sleep 1;
264         return 0;
265 }
266
267 start_c() {
268         host=$OSRF_HOSTNAME
269         if [ "_$host" = "_" ]; then
270                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
271         fi;
272
273         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
274         opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
275         return 0;
276 }
277
278 stop_c() {
279         do_action "stop" $PID_OSRF_C "OpenSRF C";
280         [ -e $PID_OSRF_C ] && rm $PID_OSRF_C;
281         sleep 1;
282         return 0;
283 }
284
285 clear_pid() {
286         echo "Clearing PID files...";
287         cd $OPT_PID_DIR;
288         [ 0 -lt ls | wc -l ] && rm -v *.pid;
289         return 0;
290 }
291
292 smart_clear() {
293         echo "Smart clearing PID files...";
294         for line in $(find $OPT_PID_DIR -name *.pid -type f)
295         do
296                 running="false";
297                 for p in $(cat $line)
298                 do
299                         [ 0 -lt $(ps ax | grep "$p" | grep -v grep | wc -l) ] && running="true";
300                 done
301                 
302                 if [ $running = "false" ]; then
303                         rm $line;
304                         echo "Removing stale PID file: $line";
305                 fi;
306         done
307         
308         return 0;
309 }
310
311 # send signal $OPT_SIGNAL to the Listener for service $OPT_SERVICE
312 # if $OPT_SERVICE = "*", signal all OpenSRF Listener services
313 send_signal() {
314     sig=$OPT_SIGNAL;
315     service=$OPT_SERVICE;
316
317     # openrsf C services do not write per-process PID files,
318     # so the simplest approach is to locate all listeners via ps/grep
319
320     found=0;
321     for svc in $(ps ax | grep "OpenSRF Listener" | grep -v grep | \
322         perl -e 'while(<>){s/.*\[(.*)\].*/$1/; print}' | sort | uniq); do
323
324         if [ "$service" = "*" -o "$service" = "$svc" ]; then
325
326             # grab the listener PIDs
327             for pid in $(ps ax | grep "OpenSRF Listener \[$svc\]" | \
328                     grep -v grep | cut -d' ' -f1); do
329                 found=1;
330                 echo "Sending signal '$sig' to $pid : $svc";
331                 kill -s $sig $pid;
332             done;
333         fi;
334     done;
335
336     if [ $found -eq 0 ]; then
337         if [ "$service" = "*" ]; then
338             echo "I have no services to signal!"
339         else
340             echo "No PID found for $service!"
341         fi;
342     fi;
343 }
344
345 # ---------------------------------------------------------------------------
346 # Do the requested action
347 # ---------------------------------------------------------------------------
348 case $OPT_ACTION in
349         "start_router") start_router;;
350         "stop_router") stop_router;;
351         "restart_router") stop_router; start_router;;
352         "start_perl") start_perl;;
353         "stop_perl") stop_perl;;
354         "restart_perl") stop_perl; start_perl;;
355         "start_python") start_python;;
356         "stop_python") stop_python;;
357         "restart_python") stop_python; start_python;;
358         "start_c") start_c;;
359         "stop_c") stop_c;;
360         "restart_c") stop_c; start_c;;
361         "start_osrf") start_perl; start_c; start_python;;
362         "stop_osrf") stop_python; stop_c; stop_perl;;
363         "restart_osrf") stop_python; stop_c; stop_perl; start_perl; start_c; start_python;;
364         "stop_all") stop_python; stop_c; stop_perl; stop_router;;
365         "start_all") start_router; start_perl; start_c; start_python;;
366         "restart_all") stop_python; stop_c; stop_perl; stop_router; start_router; start_perl; start_c; start_python;;
367         "clear_pid") clear_pid;;
368         "smart_clear") smart_clear;;
369         "signal") send_signal;;
370         "signal_all") send_signal;;
371         *) usage;;
372 esac;
373
374
375