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