adding -l for localhost support
[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:l:h" flag; do
45         case $flag in   
46                 "a")            OPT_ACTION="$OPTARG";;
47                 "c")            OPT_CONFIG="$OPTARG";;
48                 "d")            OPT_PID_DIR="$OPTARG";;
49                 "l")            export OSRF_HOSTNAME="localhost";;
50                 "h"|*)  usage;;
51         esac;
52 done
53
54
55 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=/tmp;
56 [ -z "$OPT_ACTION" ] && usage;
57
58 PID_ROUTER="$OPT_PID_DIR/router.pid";
59 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
60 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
61
62
63 # ---------------------------------------------------------------------------
64 # Utility code for checking the PID files
65 # ---------------------------------------------------------------------------
66 function do_action {
67
68         action="$1"; 
69         pidfile="$2";
70         item="$3"; 
71
72         if [ $action == "start" ]; then
73
74                 if [ -e $pidfile ]; then
75                         pid=$(cat $pidfile);
76                         echo "$item already started : $pid";
77                         return 0;
78                 fi;
79                 echo "Starting $item";
80         fi;
81
82         if [ $action == "stop" ]; then
83
84                 if [ ! -e $pidfile ]; then
85                         echo "$item not running";
86                         return 0;
87                 fi;
88
89                 pid=$(cat $pidfile);
90                 echo "Stopping $item : $pid";
91                 kill -s INT $pid 2> /dev/null;
92         sleep 1;
93                 kill -9 $pid 2> /dev/null;
94                 rm -f $pidfile;
95
96         fi;
97
98         return 0;
99 }
100
101
102 # ---------------------------------------------------------------------------
103 # Start / Stop functions
104 # ---------------------------------------------------------------------------
105
106
107 function start_router {
108         do_action "start" $PID_ROUTER "OpenSRF Router";
109         opensrf_router $OPT_CONFIG router
110         pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
111         echo $pid > $PID_ROUTER;
112         return 0;
113 }
114
115 function stop_router {
116         do_action "stop" $PID_ROUTER "OpenSRF Router";
117         return 0;
118 }
119
120 function start_perl {
121         do_action "start" $PID_OSRF_PERL "OpenSRF Perl";
122         perl -MOpenSRF::System="$OPT_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
123         sleep 3;
124         pid=$(ps ax | grep "OpenSRF System" | grep -v grep | grep -v "System-C"| awk '{print $1}')
125         echo $pid > $PID_OSRF_PERL;
126         return 0;
127 }
128
129 function stop_perl {
130         do_action "stop" $PID_OSRF_PERL "OpenSRF Perl";
131         sleep 1;
132         return 0;
133 }
134
135 function start_c {
136         host=$OSRF_HOSTNAME
137         if [ "_$host" == "_" ]; then
138                 host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()');
139         fi;
140
141         do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
142         opensrf-c $host $OPT_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