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.
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.
13 # Strictness to avoid folly
18 exec_prefix=@exec_prefix@
27 # ---------------------------------------------------------------------------
28 # Make sure we're running as the correct user
29 # ---------------------------------------------------------------------------
30 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
36 usage: $0 [OPTION]... -c <c_config> -a <action>
37 usage: $0 -a signal -k <signal> -s <service>
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.
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
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
84 # ---------------------------------------------------------------------------
85 # Load the command line options and set the global vars
86 # ---------------------------------------------------------------------------
87 while getopts "a:d:c:s:k:lh" flag; do
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";;
99 OSRF_CONFIG="@bindir@/osrf_config"
100 [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config`
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";
107 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run/opensrf;
108 [ -z "$OPT_ACTION" ] && usage;
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";
115 # apply some sanity checks to the signal handling arguments
116 if [ ${OPT_ACTION%_all} = "signal" ]; then
119 [ -z "$OPT_SIGNAL" ] && \
120 warning="-a $OPT_ACTION requires a signal specified by -k";
122 [ $OPT_ACTION = "signal" -a -z "$OPT_SERVICE" ] && \
123 warning="$warning\n-a $OPT_ACTION requires a service specified by -s";
125 [ $OPT_ACTION = "signal_all" -a -n "$OPT_SERVICE" ] && \
126 warning="$warning\n-s $OPT_SERVICE cannot be used together with $OPT_ACTION";
128 if [ -n "$warning" ]; then
135 [ $OPT_ACTION = "signal_all" ] && OPT_SERVICE="*";
138 # ---------------------------------------------------------------------------
139 # Utility code for checking the PID files
140 # ---------------------------------------------------------------------------
147 if [ $action = "start" ]; then
149 if [ -e $pidfile ]; then
151 reported=$(ps ax | grep "$item" | grep -v grep | awk '{print $1}' | awk '{ printf "%s ", $0 }')
153 if [ "$pid " = "$reported" ]; then
154 echo "$item already started : $pid";
157 echo "$item not started, but PID file exists, removing file and starting";
162 echo "Starting $item";
165 if [ $action = "stop" ]; then
167 if [ ! -e $pidfile ]; then
168 echo "$item not running";
173 if [ -n "$pid" ]; then
174 echo "Stopping $item process $pid..."
186 # ---------------------------------------------------------------------------
187 # Start / Stop functions
188 # ---------------------------------------------------------------------------
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;
201 do_action "stop" $PID_ROUTER "OpenSRF Router";
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.
209 [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
210 echo "Starting OpenSRF Python";
212 if [ -e $PID_OSRF_PYTHON ]; then #If python is started already (or it thinks so).
214 Python processes are either already running, or were not correctly shut down.
215 Now clearing any stale PID files and restarting Perl processes.
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.
230 [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
231 echo "Stopping OpenSRF Python";
232 [ -e $PID_OSRF_PYTHON ] && rm $PID_OSRF_PYTHON;
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
243 echo "Starting OpenSRF Perl";
245 if [ -e $PID_OSRF_PERL ]; then #If perl is started already (or it thinks so)
247 Perl processes are either already running, or were not correctly shut down.
248 Now clearing any stale PID files and restarting Perl processes.
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.
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;
269 if [ "_$host" = "_" ]; then
270 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
273 do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
274 opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
279 do_action "stop" $PID_OSRF_C "OpenSRF C";
280 [ -e $PID_OSRF_C ] && rm $PID_OSRF_C;
286 echo "Clearing PID files...";
288 [ 0 -lt ls | wc -l ] && rm -v *.pid;
293 echo "Smart clearing PID files...";
294 for line in $(find $OPT_PID_DIR -name *.pid -type f)
297 for p in $(cat $line)
299 [ 0 -lt $(ps ax | grep "$p" | grep -v grep | wc -l) ] && running="true";
302 if [ $running = "false" ]; then
304 echo "Removing stale PID file: $line";
311 # send signal $OPT_SIGNAL to the Listener for service $OPT_SERVICE
312 # if $OPT_SERVICE = "*", signal all OpenSRF Listener services
315 service=$OPT_SERVICE;
317 # openrsf C services do not write per-process PID files,
318 # so the simplest approach is to locate all listeners via ps/grep
321 for svc in $(ps ax | grep "OpenSRF Listener" | grep -v grep | \
322 perl -e 'while(<>){s/.*\[(.*)\].*/$1/; print}' | sort | uniq); do
324 if [ "$service" = "*" -o "$service" = "$svc" ]; then
326 # grab the listener PIDs
327 for pid in $(ps ax | grep "OpenSRF Listener \[$svc\]" | \
328 grep -v grep | cut -d' ' -f1); do
330 echo "Sending signal '$sig' to $pid : $svc";
336 if [ $found -eq 0 ]; then
337 if [ "$service" = "*" ]; then
338 echo "I have no services to signal!"
340 echo "No PID found for $service!"
345 # ---------------------------------------------------------------------------
346 # Do the requested action
347 # ---------------------------------------------------------------------------
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;;
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;;