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@
25 # ---------------------------------------------------------------------------
26 # Make sure we're running as the correct user
27 # ---------------------------------------------------------------------------
28 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
34 usage: $0 [OPTION]... -c <c_config> -a <action>
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
63 smart_clear - Clear all PID files that don't refer to a process
64 clear_pid - Clear all PID files
68 $0 -l -c opensrf_core.xml -a restart_all
73 # ---------------------------------------------------------------------------
74 # Load the command line options and set the global vars
75 # ---------------------------------------------------------------------------
76 while getopts "a:d:c:lh" flag; do
78 "a") OPT_ACTION="$OPTARG";;
79 "c") OPT_CONFIG="$OPTARG";;
80 "d") OPT_PID_DIR="$OPTARG";;
81 "l") export OSRF_HOSTNAME="localhost";;
86 OSRF_CONFIG="@bindir@/osrf_config"
87 [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config`
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";
94 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run/opensrf;
95 [ -z "$OPT_ACTION" ] && usage;
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";
103 # ---------------------------------------------------------------------------
104 # Utility code for checking the PID files
105 # ---------------------------------------------------------------------------
112 if [ $action = "start" ]; then
114 if [ -e $pidfile ]; then
116 reported=$(ps ax | grep "$item" | grep -v grep | awk '{print $1}' | awk '{ printf "%s ", $0 }')
118 if [ "$pid " = "$reported" ]; then
119 echo "$item already started : $pid";
122 echo "$item not started, but PID file exists, removing file and starting";
127 echo "Starting $item";
130 if [ $action = "stop" ]; then
132 if [ ! -e $pidfile ]; then
133 echo "$item not running";
138 if [ -n "$pid" ]; then
139 echo "Stopping $item process $pid..."
151 # ---------------------------------------------------------------------------
152 # Start / Stop functions
153 # ---------------------------------------------------------------------------
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;
166 do_action "stop" $PID_ROUTER "OpenSRF Router";
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.
174 [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
175 echo "Starting OpenSRF Python";
177 if [ -e $PID_OSRF_PYTHON ]; then #If python is started already (or it thinks so).
179 Python processes are either already running, or were not correctly shut down.
180 Now clearing any stale PID files and restarting Perl processes.
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.
195 [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return;
196 echo "Stopping OpenSRF Python";
197 [ -e $PID_OSRF_PYTHON ] && rm $PID_OSRF_PYTHON;
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
208 echo "Starting OpenSRF Perl";
210 if [ -e $PID_OSRF_PERL ]; then #If perl is started already (or it thinks so)
212 Perl processes are either already running, or were not correctly shut down.
213 Now clearing any stale PID files and restarting Perl processes.
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.
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;
234 if [ "_$host" = "_" ]; then
235 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
238 do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
239 opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
244 do_action "stop" $PID_OSRF_C "OpenSRF C";
245 [ -e $PID_OSRF_C ] && rm $PID_OSRF_C;
251 echo "Clearing PID files...";
253 [ 0 -lt ls | wc -l ] && rm -v *.pid;
258 echo "Smart clearing PID files...";
259 for line in $(find $OPT_PID_DIR -name *.pid -type f)
262 for p in $(cat $line)
264 [ 0 -lt $(ps ax | grep "$p" | grep -v grep | wc -l) ] && running="true";
267 if [ $running = "false" ]; then
269 echo "Removing stale PID file: $line";
276 # ---------------------------------------------------------------------------
277 # Do the requested action
278 # ---------------------------------------------------------------------------
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;;
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;;