better, more-flexible, easier to use, all in one control script
[OpenSRF.git] / bin / osrf_ctl.sh
1 #!/bin/bash
2
3 # NOTE: Eventually, there will be one OpenSRF config file format
4 # When this happens, we will only need a single OPT_CONFIG variable
5 # Also note that PIDs are collect from ps/grep/awk for C commands because
6 # they fork internally and there's no way to find their PIDs at launch time
7 # This is hackish and likely non-portable
8
9 OPT_ACTION="" 
10 OPT_PERL_CONFIG=""
11 OPT_C_CONFIG=""
12 OPT_PID_DIR=""
13
14 function usage {
15         echo "usage: $0 -d <pid_dir> -p <perl_config> -c <c_config> -a <action>";
16         exit;
17 }
18
19
20 while getopts  "p:c:a:d:h" flag; do
21         case $flag in   
22                 "a")            OPT_ACTION="$OPTARG";;
23                 "c")            OPT_C_CONFIG="$OPTARG";;
24                 "p")            OPT_PERL_CONFIG="$OPTARG";;
25                 "d")            OPT_PID_DIR="$OPTARG";;
26                 "h"|*)  usage;;
27         esac;
28 done
29
30 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=/tmp;
31 [ -z "$OPT_ACTION" ] && usage;
32
33
34 PID_ROUTER="$OPT_PID_DIR/router.pid";
35 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
36 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
37
38
39 # ---------------------------------------------------------------------------
40 # Start / Stop functions
41 # ---------------------------------------------------------------------------
42 function start_router {
43         if [ -e "$PID_ROUTER" ]; then
44                 pid=$(cat "$PID_ROUTER");
45                 echo "Router is already running : $pid" && return 0;
46         fi;
47         echo "Starting router...";
48         opensrf_router $OPT_C_CONFIG router
49         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
50         echo $pid > $PID_ROUTER;
51         return 0;
52 }
53
54 function stop_router {
55         [ ! -e "$PID_ROUTER" ] && echo "Router is not running" && return 0;
56         pid=$(cat "$PID_ROUTER");
57         echo "Stopping router : $pid";
58         kill -s INT $pid;
59         rm "$PID_ROUTER";
60         return 0;
61 }
62
63 function start_perl {
64         if [ -e "$PID_OSRF_PERL" ]; then
65                 pid=$(cat "$PID_OSRF_PERL");
66                 echo "OpenSRF perl is already running : $pid" && return 0;
67         fi;
68         perl -MOpenSRF::System="$OPT_PERL_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
69         pid=$!;
70         echo $pid > $PID_OSRF_PERL;
71         return 0;
72 }
73
74 function stop_perl {
75         [ ! -e "$PID_OSRF_PERL" ] && echo "OpenSRF-Perl is not running" && return 0;
76         pid=$(cat "$PID_OSRF_PERL");
77         echo "Stopping perl : $pid";
78         kill -s INT $pid;
79         rm "$PID_OSRF_PERL";
80         return 0;
81 }
82
83 function start_c {
84         if [ -e "$PID_OSRF_C" ]; then
85                 pid=$(cat "$PID_OSRF_C");
86                 echo "OpenSRF-C is already running : $pid" && return 0;
87         fi;
88         echo "Starting OpenSRF C";
89         opensrf-c $(hostname -f) $OPT_C_CONFIG opensrf;
90         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
91         echo $pid > "$PID_OSRF_C";
92         return 0;
93 }
94
95 function stop_c {
96         [ ! -e "$PID_OSRF_C" ] && echo "OpenSRF-C is not running" && return 0;
97         pid=$(cat "$PID_OSRF_C");
98         echo "Stopping OpenSRF C : $pid";
99         #kill -9 $pid;
100         killall -9 opensrf-c  # hack for now
101         rm $PID_OSRF_C;
102         return 0;
103 }
104
105
106
107 # ---------------------------------------------------------------------------
108 # Do the requested action
109 # ---------------------------------------------------------------------------
110 case $OPT_ACTION in
111         "start_router") start_router;;
112         "stop_router") stop_router;;
113         "start_perl") start_perl;;
114         "stop_perl") stop_perl;;
115         "restart_perl") stop_perl && sleep 1 && start_perl;;
116         "start_c") start_c;;
117         "stop_c") stop_c;;
118         "restart_c") stop_c && sleep 1 && start_c;;
119         "start_osrf") start_perl && sleep 5 && start_c;;
120         "stop_osrf") stop_perl; stop_c;;
121         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
122         "stop_all") stop_c; stop_perl; stop_router;;
123         "start_all") start_router; start_perl && sleep 5 && start_c;;
124         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl && sleep 5 && start_c;;
125         *) usage;;
126 esac;
127
128
129