]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh.in
update ChangeLog for 2.2.1
[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 OSRF_HOSTNAME=""
24
25 # ---------------------------------------------------------------------------
26 # Make sure we're running as the correct user
27 # ---------------------------------------------------------------------------
28 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
29
30
31 usage() {
32         cat << EOF
33
34 usage: $0 [OPTION]... -c <c_config> -a <action>
35
36 Mandatory parameters:
37   -a    action to perform
38
39 Optional parameters:";
40   -c    full path to C configuration file (opensrf_core.xml)
41   -d    store PID files in this directory
42   -l    accept 'localhost' as the fully-qualified domain name
43
44 Actions include:
45     start_router
46     stop_router
47     restart_router
48     start_perl
49     stop_perl
50     restart_perl
51     start_python
52     stop_python
53     restart_python
54     start_c
55     stop_c
56     restart_c
57     start_osrf
58     stop_osrf
59     restart_osrf
60     stop_all
61     start_all
62     restart_all
63     smart_clear     - Clear all PID files that don't refer to a process 
64     clear_pid       - Clear all PID files
65
66 Examples:
67   $0 -a restart_all
68   $0 -l -c opensrf_core.xml -a restart_all
69
70 EOF
71 }
72
73 # ---------------------------------------------------------------------------
74 # Load the command line options and set the global vars
75 # ---------------------------------------------------------------------------
76 while getopts  "a:d:c:lh" flag; do
77         case $flag in   
78                 "a")            OPT_ACTION="$OPTARG";;
79                 "c")            OPT_CONFIG="$OPTARG";;
80                 "d")            OPT_PID_DIR="$OPTARG";;
81                 "l")            export OSRF_HOSTNAME="localhost";;
82                 "h"|*)  usage;;
83         esac;
84 done
85
86 OSRF_CONFIG="@bindir@/osrf_config"
87 [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config`
88
89 [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml;
90 if [ ! -r "$OPT_CONFIG" ]; then
91         echo "Please specify the location of the opensrf_core.xml file using the -c flag";
92         exit 1;
93 fi;
94 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run/opensrf;
95 [ -z "$OPT_ACTION" ] && usage;
96
97 PID_ROUTER="$OPT_PID_DIR/router.pid";
98 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
99 PID_OSRF_PYTHON="$OPT_PID_DIR/osrf_python.pid";
100 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
101
102
103 # ---------------------------------------------------------------------------
104 # Utility code for checking the PID files
105 # ---------------------------------------------------------------------------
106 do_action() {
107
108         action="$1"; 
109         pidfile="$2";
110         item="$3"; 
111
112         if [ $action = "start" ]; then
113
114                 if [ -e $pidfile ]; then
115                         pid=$(cat $pidfile);
116                         reported=$(ps ax | grep "$item" | grep -v grep | awk '{print $1}' | awk '{ printf "%s ", $0 }')
117                         
118                         if [ "$pid " = "$reported" ]; then
119                                 echo "$item already started : $pid";
120                                 return 0;
121                         else
122                                 echo "$item not started, but PID file exists, removing file and starting";
123                                 rm $pidfile;
124                                 return 0;
125                         fi;
126                 fi;
127                 echo "Starting $item";
128         fi;
129
130         if [ $action = "stop" ]; then
131
132                 if [ ! -e $pidfile ]; then
133                         echo "$item not running";
134                         return 0;
135                 fi;
136
137         while read pid; do
138             if [ -n "$pid" ]; then
139                 echo "Stopping $item process $pid..."
140                 kill -s INT $pid
141             fi;
142         done < $pidfile;
143                 rm -f $pidfile;
144
145         fi;
146
147         return 0;
148 }
149
150
151 # ---------------------------------------------------------------------------
152 # Start / Stop functions
153 # ---------------------------------------------------------------------------
154
155
156 start_router() {
157         do_action "start" $PID_ROUTER "OpenSRF Router";
158         opensrf_router $OPT_CONFIG routers
159     sleep 2; # give the router procs time to fork and appear in ps
160         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
161         echo $pid > $PID_ROUTER;
162         return 0;
163 }
164
165 stop_router() {
166         do_action "stop" $PID_ROUTER "OpenSRF Router";
167         return 0;
168 }
169
170 # The clean way to do this would be to invoke the "start_all" action
171 # on opensrf.py, but that seems to randomly fail for services. So we
172 # start each one separately from a bash script instead.
173 start_python() {
174     [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
175     echo "Starting OpenSRF Python";
176     
177     if [ -e $PID_OSRF_PYTHON ]; then  #If python is started already (or it thinks so).
178     cat << EOF
179 Python processes are either already running, or were not correctly shut down.
180 Now clearing any stale PID files and restarting Perl processes.
181 EOF
182     smart_clear;
183     fi;
184
185     OPT_LOCAL=""
186     [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l"
187     for service in `opensrf.py -a list_all $OPT_LOCAL`; do
188             opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -d -a start -s $service $OPT_LOCAL
189             [ "$?" = "0" ] && echo "Python Started" > $PID_OSRF_PYTHON;  #Dummy pid file, removed when a proper shutdown happens.
190     done
191     return 0;
192 }
193
194 stop_python() {
195     [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
196     echo "Stopping OpenSRF Python";
197     [ -e $PID_OSRF_PYTHON ] && rm $PID_OSRF_PYTHON;
198     OPT_LOCAL=""
199     [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l"
200     set +e # Ignore errors for NXDOMAIN
201     opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -a stop_all $OPT_LOCAL
202     set -e # Errors matter again
203     sleep 1;
204     return 0;
205 }
206
207 start_perl() {
208     echo "Starting OpenSRF Perl";
209     
210     if [ -e $PID_OSRF_PERL ]; then  #If perl is started already (or it thinks so)
211     cat << EOF
212 Perl processes are either already running, or were not correctly shut down.
213 Now clearing any stale PID files and restarting Perl processes.
214 EOF
215     smart_clear;
216     fi;
217     
218     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
219         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
220     [ "$?" = "0" ] && echo "Perl Started" > $PID_OSRF_PERL;  #Dummy pid file, removed when a proper shutdown happens.
221         return 0;
222 }
223
224 stop_perl() {
225     echo "Stopping OpenSRF Perl";
226     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
227     [ -e $PID_OSRF_PERL ] && rm $PID_OSRF_PERL;
228         sleep 1;
229         return 0;
230 }
231
232 start_c() {
233         host=$OSRF_HOSTNAME
234         if [ "_$host" = "_" ]; then
235                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
236         fi;
237
238         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
239         opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
240         return 0;
241 }
242
243 stop_c() {
244         do_action "stop" $PID_OSRF_C "OpenSRF C";
245         [ -e $PID_OSRF_C ] && rm $PID_OSRF_C;
246         sleep 1;
247         return 0;
248 }
249
250 clear_pid() {
251         echo "Clearing PID files...";
252         cd $OPT_PID_DIR;
253         [ 0 -lt ls | wc -l ] && rm -v *.pid;
254         return 0;
255 }
256
257 smart_clear() {
258         echo "Smart clearing PID files...";
259         for line in $(find $OPT_PID_DIR -name *.pid -type f)
260         do
261                 running="false";
262                 for p in $(cat $line)
263                 do
264                         [ 0 -lt $(ps ax | grep "$p" | grep -v grep | wc -l) ] && running="true";
265                 done
266                 
267                 if [ $running = "false" ]; then
268                         rm $line;
269                         echo "Removing stale PID file: $line";
270                 fi;
271         done
272         
273         return 0;
274 }
275
276 # ---------------------------------------------------------------------------
277 # Do the requested action
278 # ---------------------------------------------------------------------------
279 case $OPT_ACTION in
280         "start_router") start_router;;
281         "stop_router") stop_router;;
282         "restart_router") stop_router; start_router;;
283         "start_perl") start_perl;;
284         "stop_perl") stop_perl;;
285         "restart_perl") stop_perl; start_perl;;
286         "start_python") start_python;;
287         "stop_python") stop_python;;
288         "restart_python") stop_python; start_python;;
289         "start_c") start_c;;
290         "stop_c") stop_c;;
291         "restart_c") stop_c; start_c;;
292         "start_osrf") start_perl; start_c; start_python;;
293         "stop_osrf") stop_python; stop_c; stop_perl;;
294         "restart_osrf") stop_python; stop_c; stop_perl; start_perl; start_c; start_python;;
295         "stop_all") stop_python; stop_c; stop_perl; stop_router;;
296         "start_all") start_router; start_perl; start_c; start_python;;
297         "restart_all") stop_python; stop_c; stop_perl; stop_router; start_router; start_perl; start_c; start_python;;
298         "clear_pid") clear_pid;;
299         "smart_clear") smart_clear;;
300         *) usage;;
301 esac;
302
303
304