]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh.in
1. In endElementHandler(), responding to an error: don't explain what a
[OpenSRF.git] / bin / osrf_ctl.sh.in
1 #!/bin/bash
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 function usage {
32         echo "";
33         echo "usage: $0 [OPTION]... -c <c_config> -a <action>";
34         echo "";
35         echo "Mandatory parameters:";
36         echo -e "  -a\t\taction to perform";
37         echo "";
38         echo "Optional parameters:";
39         echo -e "  -c\t\tfull path to C configuration file (opensrf_core.xml)";
40         echo -e "  -d\t\tstore PID files in this directory";
41         echo -e "  -l\t\taccept 'localhost' as the fully-qualified domain name";
42         echo "";
43         echo "Actions include:";
44         echo -e "\tstart_router"
45         echo -e "\tstop_router"
46         echo -e "\trestart_router"
47         echo -e "\tstart_perl"
48         echo -e "\tstop_perl"
49         echo -e "\trestart_perl"
50         echo -e "\tstart_c"
51         echo -e "\tstop_c"
52         echo -e "\trestart_c"
53         echo -e "\tstart_osrf"
54         echo -e "\tstop_osrf"
55         echo -e "\trestart_osrf"
56         echo -e "\tstop_all" 
57         echo -e "\tstart_all"
58         echo -e "\trestart_all"
59         echo "";
60         echo "Examples:";
61         echo "  $0 -a restart_all";
62         echo "  $0 -l -c opensrf_core.xml -a restart_all";
63         echo "";
64         exit;
65 }
66
67 # ---------------------------------------------------------------------------
68 # Load the command line options and set the global vars
69 # ---------------------------------------------------------------------------
70 while getopts  "a:d:c:lh" flag; do
71         case $flag in   
72                 "a")            OPT_ACTION="$OPTARG";;
73                 "c")            OPT_CONFIG="$OPTARG";;
74                 "d")            OPT_PID_DIR="$OPTARG";;
75                 "l")            export OSRF_HOSTNAME="localhost";;
76                 "h"|*)  usage;;
77         esac;
78 done
79
80 OSRF_CONFIG="@bindir@/osrf_config"
81 [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config`
82
83 [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml;
84 if [ ! -r "$OPT_CONFIG" ]; then
85         echo "Please specify the location of the opensrf_core.xml file using the -c flag";
86         exit 1;
87 fi;
88 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run;
89 [ -z "$OPT_ACTION" ] && usage;
90
91 PID_ROUTER="$OPT_PID_DIR/router.pid";
92 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
93 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
94
95
96 # ---------------------------------------------------------------------------
97 # Utility code for checking the PID files
98 # ---------------------------------------------------------------------------
99 function do_action {
100
101         action="$1"; 
102         pidfile="$2";
103         item="$3"; 
104
105         if [ $action == "start" ]; then
106
107                 if [ -e $pidfile ]; then
108                         pid=$(cat $pidfile);
109                         echo "$item already started : $pid";
110                         return 0;
111                 fi;
112                 echo "Starting $item";
113         fi;
114
115         if [ $action == "stop" ]; then
116
117                 if [ ! -e $pidfile ]; then
118                         echo "$item not running";
119                         return 0;
120                 fi;
121
122         while read pid; do
123             if [ -n "$pid" ]; then
124                 echo "Stopping $item process $pid..."
125                 kill -s INT $pid
126             fi;
127         done < $pidfile;
128                 rm -f $pidfile;
129
130         fi;
131
132         return 0;
133 }
134
135
136 # ---------------------------------------------------------------------------
137 # Start / Stop functions
138 # ---------------------------------------------------------------------------
139
140
141 function start_router {
142         do_action "start" $PID_ROUTER "OpenSRF Router";
143         opensrf_router $OPT_CONFIG routers
144     sleep 2; # give the router procs time to fork and appear in ps
145         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
146         echo $pid > $PID_ROUTER;
147         return 0;
148 }
149
150 function stop_router {
151         do_action "stop" $PID_ROUTER "OpenSRF Router";
152         return 0;
153 }
154
155 function start_perl {
156     echo "Starting OpenSRF Perl";
157     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \
158         --config $OPT_CONFIG --action start_all --settings-startup-pause 3
159         return 0;
160 }
161
162 function stop_perl {
163     echo "Stopping OpenSRF Perl";
164     opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all
165         sleep 1;
166         return 0;
167 }
168
169 function start_c {
170         host=$OSRF_HOSTNAME
171         if [ "_$host" == "_" ]; then
172                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
173         fi;
174
175         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
176         opensrf-c $host $OPT_CONFIG opensrf;
177     sleep 1; # give the main C proc time to appear in ps
178         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
179         echo $pid > "$PID_OSRF_C";
180         return 0;
181 }
182
183 function stop_c {
184         do_action "stop" $PID_OSRF_C "OpenSRF C";
185         sleep 1;
186         return 0;
187 }
188
189
190
191 # ---------------------------------------------------------------------------
192 # Do the requested action
193 # ---------------------------------------------------------------------------
194 case $OPT_ACTION in
195         "start_router") start_router;;
196         "stop_router") stop_router;;
197         "restart_router") stop_router; start_router;;
198         "start_perl") start_perl;;
199         "stop_perl") stop_perl;;
200         "restart_perl") stop_perl; start_perl;;
201         "start_c") start_c;;
202         "stop_c") stop_c;;
203         "restart_c") stop_c; start_c;;
204         "start_osrf") start_perl; start_c;;
205         "stop_osrf") stop_perl; stop_c;;
206         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
207         "stop_all") stop_c; stop_perl; stop_router;;
208         "start_all") start_router; start_perl; start_c;;
209         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
210         *) usage;;
211 esac;
212
213
214