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