]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh.in
Improved PID file handling in osrf_ctl.sh
[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     opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -a stop_all $OPT_LOCAL
201     sleep 1;
202     return 0;
203 }
204
205 start_perl() {
206     echo "Starting OpenSRF Perl";
207     
208     if [ -e $PID_OSRF_PERL ]; then  #If perl is started already (or it thinks so)
209     cat << EOF
210 Perl processes are either already running, or were not correctly shut down.
211 Now clearing any stale PID files and restarting Perl processes.
212 EOF
213     smart_clear;
214     fi;
215     
216     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
217         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
218     [ "$?" = "0" ] && echo "Perl Started" > $PID_OSRF_PERL;  #Dummy pid file, removed when a proper shutdown happens.
219         return 0;
220 }
221
222 stop_perl() {
223     echo "Stopping OpenSRF Perl";
224     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
225     [ -e $PID_OSRF_PERL ] && rm $PID_OSRF_PERL;
226         sleep 1;
227         return 0;
228 }
229
230 start_c() {
231         host=$OSRF_HOSTNAME
232         if [ "_$host" = "_" ]; then
233                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
234         fi;
235
236         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
237         opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
238         return 0;
239 }
240
241 stop_c() {
242         do_action "stop" $PID_OSRF_C "OpenSRF C";
243         [ -e $PID_OSRF_C ] && rm $PID_OSRF_C;
244         sleep 1;
245         return 0;
246 }
247
248 clear_pid() {
249         echo "Clearing PID files...";
250         cd $OPT_PID_DIR;
251         [ 0 -lt ls | wc -l ] && rm -v *.pid;
252         return 0;
253 }
254
255 smart_clear() {
256         echo "Smart clearing PID files...";
257         for line in $(find $OPT_PID_DIR -name *.pid -type f)
258         do
259                 running="false";
260                 for p in $(cat $line)
261                 do
262                         [ 0 -lt $(ps ax | grep "$p" | grep -v grep | wc -l) ] && running="true";
263                 done
264                 
265                 if [ $running = "false" ]; then
266                         rm $line;
267                         echo "Removing stale PID file: $line";
268                 fi;
269         done
270         
271         return 0;
272 }
273
274 # ---------------------------------------------------------------------------
275 # Do the requested action
276 # ---------------------------------------------------------------------------
277 case $OPT_ACTION in
278         "start_router") start_router;;
279         "stop_router") stop_router;;
280         "restart_router") stop_router; start_router;;
281         "start_perl") start_perl;;
282         "stop_perl") stop_perl;;
283         "restart_perl") stop_perl; start_perl;;
284         "start_python") start_python;;
285         "stop_python") stop_python;;
286         "restart_python") stop_python; start_python;;
287         "start_c") start_c;;
288         "stop_c") stop_c;;
289         "restart_c") stop_c; start_c;;
290         "start_osrf") start_perl; start_c; start_python;;
291         "stop_osrf") stop_python; stop_c; start_perl;;
292         "restart_osrf") stop_python; stop_c; stop_perl; start_perl; start_c; start_python;;
293         "stop_all") stop_python; stop_c; stop_perl; stop_router;;
294         "start_all") start_router; start_perl; start_c; start_python;;
295         "restart_all") stop_python; stop_c; stop_perl; stop_router; start_router; start_perl; start_c; start_python;;
296         "clear_pid") clear_pid;;
297         "smart_clear") smart_clear;;
298         *) usage;;
299 esac;
300
301
302