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