applied patch from Dan to provide an example usage
[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     echo "Example:";
39     echo "  $0 -p bootstrap.conf -c opensrf_core.xml -a restart_all";
40     echo "";
41         exit;
42 }
43
44
45 # ---------------------------------------------------------------------------
46 # Load the command line options and set the global vars
47 # ---------------------------------------------------------------------------
48 while getopts  "p:c:a:d:h" flag; do
49         case $flag in   
50                 "a")            OPT_ACTION="$OPTARG";;
51                 "c")            OPT_C_CONFIG="$OPTARG";;
52                 "p")            OPT_PERL_CONFIG="$OPTARG";;
53                 "d")            OPT_PID_DIR="$OPTARG";;
54                 "h"|*)  usage;;
55         esac;
56 done
57
58
59 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=/tmp;
60 [ -z "$OPT_ACTION" ] && usage;
61
62 PID_ROUTER="$OPT_PID_DIR/router.pid";
63 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
64 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
65
66
67 # ---------------------------------------------------------------------------
68 # Utility code for checking the PID files
69 # ---------------------------------------------------------------------------
70 function do_action {
71
72         action="$1"; 
73         pidfile="$2";
74         item="$3"; 
75
76         if [ $action == "start" ]; then
77
78                 if [ -e $pidfile ]; then
79                         pid=$(cat $pidfile);
80                         echo "$item already started : $pid";
81                         return 0;
82                 fi;
83                 echo "Starting $item";
84         fi;
85
86         if [ $action == "stop" ]; then
87
88                 if [ ! -e $pidfile ]; then
89                         echo "$item not running";
90                         return 0;
91                 fi;
92
93                 pid=$(cat $pidfile);
94                 echo "Stopping $item : $pid";
95                 kill -s INT $pid 2> /dev/null;
96         sleep 1;
97                 kill -9 $pid 2> /dev/null;
98                 rm -f $pidfile;
99
100         fi;
101
102         return 0;
103 }
104
105
106 # ---------------------------------------------------------------------------
107 # Start / Stop functions
108 # ---------------------------------------------------------------------------
109
110
111 function start_router {
112         do_action "start" $PID_ROUTER "OpenSRF Router";
113         opensrf_router $OPT_C_CONFIG router
114         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
115         echo $pid > $PID_ROUTER;
116         return 0;
117 }
118
119 function stop_router {
120         do_action "stop" $PID_ROUTER "OpenSRF Router";
121         return 0;
122 }
123
124 function start_perl {
125         do_action "start" $PID_OSRF_PERL "OpenSRF Perl";
126         perl -MOpenSRF::System="$OPT_PERL_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
127         pid=$!;
128         echo $pid > $PID_OSRF_PERL;
129         sleep 5;
130         return 0;
131 }
132
133 function stop_perl {
134         do_action "stop" $PID_OSRF_PERL "OpenSRF Perl";
135         sleep 1;
136         return 0;
137 }
138
139 function start_c {
140         host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()')
141         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
142         opensrf-c $host $OPT_C_CONFIG opensrf;
143         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
144         echo $pid > "$PID_OSRF_C";
145         return 0;
146 }
147
148 function stop_c {
149         do_action "stop" $PID_OSRF_C "OpenSRF C";
150         killall -9 opensrf-c  # hack for now to force kill all C services
151         sleep 1;
152         return 0;
153 }
154
155
156
157 # ---------------------------------------------------------------------------
158 # Do the requested action
159 # ---------------------------------------------------------------------------
160 case $OPT_ACTION in
161         "start_router") start_router;;
162         "stop_router") stop_router;;
163         "restart_router") stop_router; start_router;;
164         "start_perl") start_perl;;
165         "stop_perl") stop_perl;;
166         "restart_perl") stop_perl; start_perl;;
167         "start_c") start_c;;
168         "stop_c") stop_c;;
169         "restart_c") stop_c; start_c;;
170         "start_osrf") start_perl; start_c;;
171         "stop_osrf") stop_perl; stop_c;;
172         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
173         "stop_all") stop_c; stop_perl; stop_router;;
174         "start_all") start_router; start_perl; start_c;;
175         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
176         *) usage;;
177 esac;
178
179
180