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 opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -a stop_all $OPT_LOCAL
206 echo "Starting OpenSRF Perl";
208 if [ -e $PID_OSRF_PERL ]; then #If perl is started already (or it thinks so)
210 Perl processes are either already running, or were not correctly shut down.
211 Now clearing any stale PID files and restarting Perl processes.
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.
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;
232 if [ "_$host" = "_" ]; then
233 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
236 do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
237 opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C";
242 do_action "stop" $PID_OSRF_C "OpenSRF C";
243 [ -e $PID_OSRF_C ] && rm $PID_OSRF_C;
249 echo "Clearing PID files...";
251 [ 0 -lt ls | wc -l ] && rm -v *.pid;
256 echo "Smart clearing PID files...";
257 for line in $(find $OPT_PID_DIR -name *.pid -type f)
260 for p in $(cat $line)
262 [ 0 -lt $(ps ax | grep "$p" | grep -v grep | wc -l) ] && running="true";
265 if [ $running = "false" ]; then
267 echo "Removing stale PID file: $line";
274 # ---------------------------------------------------------------------------
275 # Do the requested action
276 # ---------------------------------------------------------------------------
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;;
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;;