added a ":" to the getopt "c" option so it would take a param
[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                 pid=$(cat $pidfile);
119                 echo "Stopping $item : $pid";
120                 kill -s INT $pid 2> /dev/null;
121         sleep 1;
122                 kill -9 $pid 2> /dev/null;
123                 rm -f $pidfile;
124
125         fi;
126
127         return 0;
128 }
129
130
131 # ---------------------------------------------------------------------------
132 # Start / Stop functions
133 # ---------------------------------------------------------------------------
134
135
136 function start_router {
137         do_action "start" $PID_ROUTER "OpenSRF Router";
138         opensrf_router $OPT_CONFIG router
139         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
140         echo $pid > $PID_ROUTER;
141         return 0;
142 }
143
144 function stop_router {
145         do_action "stop" $PID_ROUTER "OpenSRF Router";
146         return 0;
147 }
148
149 function start_perl {
150         do_action "start" $PID_OSRF_PERL "OpenSRF Perl";
151         perl -MOpenSRF::System="$OPT_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
152         sleep 3;
153         pid=$(ps ax | grep "OpenSRF System" | grep -v grep | grep -v "System-C"| awk '{print $1}')
154         echo $pid > $PID_OSRF_PERL;
155         return 0;
156 }
157
158 function stop_perl {
159         do_action "stop" $PID_OSRF_PERL "OpenSRF Perl";
160         sleep 1;
161         return 0;
162 }
163
164 function start_c {
165         host=$OSRF_HOSTNAME
166         if [ "_$host" == "_" ]; then
167                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
168         fi;
169
170         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
171         opensrf-c $host $OPT_CONFIG opensrf;
172         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
173         echo $pid > "$PID_OSRF_C";
174         return 0;
175 }
176
177 function stop_c {
178         do_action "stop" $PID_OSRF_C "OpenSRF C";
179         killall -9 opensrf-c  # hack for now to force kill all C services
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