spacing adjustment
[OpenSRF.git] / bin / osrf_ctl.sh
1 #!/bin/bash
2
3 OPT_ACTION=""
4 OPT_PERL_CONFIG=""
5 OPT_C_CONFIG=""
6 OPT_PID_DIR=""
7
8 # ---------------------------------------------------------------------------
9 # Make sure we're running as the correct user
10 # ---------------------------------------------------------------------------
11 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
12
13
14 # NOTE: Eventually, there will be one OpenSRF config file format
15 # When this happens, we will only need a single OPT_CONFIG variable
16
17 function usage {
18         echo "";
19         echo "usage: $0 -d <pid_dir> -p <perl_config> -c <c_config> -a <action>";
20         echo "";
21         echo "Actions include:"
22         echo -e "\tstart_router"
23         echo -e "\tstop_router"
24         echo -e "\trestart_router"
25         echo -e "\tstart_perl"
26         echo -e "\tstop_perl"
27         echo -e "\trestart_perl"
28         echo -e "\tstart_c"
29         echo -e "\tstop_c"
30         echo -e "\trestart_c"
31         echo -e "\tstart_osrf"
32         echo -e "\tstop_osrf"
33         echo -e "\trestart_osrf"
34         echo -e "\tstop_all" 
35         echo -e "\tstart_all"
36         echo -e "\trestart_all"
37         echo "";
38         exit;
39 }
40
41
42 # ---------------------------------------------------------------------------
43 # Load the command line options and set the global vars
44 # ---------------------------------------------------------------------------
45 while getopts  "p:c:a:d:h" flag; do
46         case $flag in   
47                 "a")            OPT_ACTION="$OPTARG";;
48                 "c")            OPT_C_CONFIG="$OPTARG";;
49                 "p")            OPT_PERL_CONFIG="$OPTARG";;
50                 "d")            OPT_PID_DIR="$OPTARG";;
51                 "h"|*)  usage;;
52         esac;
53 done
54
55
56 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=/tmp;
57 [ -z "$OPT_ACTION" ] && usage;
58
59 PID_ROUTER="$OPT_PID_DIR/router.pid";
60 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
61 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
62
63
64 # ---------------------------------------------------------------------------
65 # Utility code for checking the PID files
66 # ---------------------------------------------------------------------------
67 function do_action {
68
69         action="$1"; 
70         pidfile="$2";
71         item="$3"; 
72
73         if [ $action == "start" ]; then
74
75                 if [ -e $pidfile ]; then
76                         pid=$(cat $pidfile);
77                         echo "$item already started : $pid";
78                         return 0;
79                 fi;
80                 echo "Starting $item";
81         fi;
82
83         if [ $action == "stop" ]; then
84
85                 if [ ! -e $pidfile ]; then
86                         echo "$item not running";
87                         return 0;
88                 fi;
89
90                 pid=$(cat $pidfile);
91                 echo "Stopping $item : $pid";
92                 kill -s INT $pid 2> /dev/null;
93         sleep 1;
94                 kill -9 $pid 2> /dev/null;
95                 rm -f $pidfile;
96
97         fi;
98
99         return 0;
100 }
101
102
103 # ---------------------------------------------------------------------------
104 # Start / Stop functions
105 # ---------------------------------------------------------------------------
106
107
108 function start_router {
109         do_action "start" $PID_ROUTER "OpenSRF Router";
110         opensrf_router $OPT_C_CONFIG router
111         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
112         echo $pid > $PID_ROUTER;
113         return 0;
114 }
115
116 function stop_router {
117         do_action "stop" $PID_ROUTER "OpenSRF Router";
118         return 0;
119 }
120
121 function start_perl {
122         do_action "start" $PID_OSRF_PERL "OpenSRF Perl";
123         perl -MOpenSRF::System="$OPT_PERL_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
124         pid=$!;
125         echo $pid > $PID_OSRF_PERL;
126         sleep 5;
127         return 0;
128 }
129
130 function stop_perl {
131         do_action "stop" $PID_OSRF_PERL "OpenSRF Perl";
132         sleep 1;
133         return 0;
134 }
135
136 function start_c {
137         host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()')
138         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
139         opensrf-c $host $OPT_C_CONFIG opensrf;
140         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
141         echo $pid > "$PID_OSRF_C";
142         return 0;
143 }
144
145 function stop_c {
146         do_action "stop" $PID_OSRF_C "OpenSRF C";
147         killall -9 opensrf-c  # hack for now to force kill all C services
148         sleep 1;
149         return 0;
150 }
151
152
153
154 # ---------------------------------------------------------------------------
155 # Do the requested action
156 # ---------------------------------------------------------------------------
157 case $OPT_ACTION in
158         "start_router") start_router;;
159         "stop_router") stop_router;;
160         "restart_router") stop_router; start_router;;
161         "start_perl") start_perl;;
162         "stop_perl") stop_perl;;
163         "restart_perl") stop_perl; start_perl;;
164         "start_c") start_c;;
165         "stop_c") stop_c;;
166         "restart_c") stop_c; start_c;;
167         "start_osrf") start_perl; start_c;;
168         "stop_osrf") stop_perl; stop_c;;
169         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
170         "stop_all") stop_c; stop_perl; stop_router;;
171         "start_all") start_router; start_perl; start_c;;
172         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
173         *) usage;;
174 esac;
175
176
177