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