]> git.evergreen-ils.org Git - OpenSRF.git/blob - bin/osrf_ctl.sh
dies if user is not "opensrf"
[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) != 'openesrf' ] && 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         exit;
39 }
40
41
42 # ---------------------------------------------------------------------------
43 # Load the command line options and set the global vars
44 # ---------------------------------------------------------------------------
45 while getopts  "p:c:a:d:h" flag; do
46         case $flag in   
47                 "a")            OPT_ACTION="$OPTARG";;
48                 "c")            OPT_C_CONFIG="$OPTARG";;
49                 "p")            OPT_PERL_CONFIG="$OPTARG";;
50                 "d")            OPT_PID_DIR="$OPTARG";;
51                 "h"|*)  usage;;
52         esac;
53 done
54
55
56 [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=/tmp;
57 [ -z "$OPT_ACTION" ] && usage;
58
59 PID_ROUTER="$OPT_PID_DIR/router.pid";
60 PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid";
61 PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid";
62
63
64 # ---------------------------------------------------------------------------
65 # Utility code for checking the PID files
66 # ---------------------------------------------------------------------------
67 function do_action {
68
69         action="$1"; 
70         pidfile="$2";
71         item="$3"; 
72
73         if [ $action == "start" ]; then
74
75                 if [ -e $pidfile ]; then
76                         pid=$(cat $pidfile);
77                         echo "$item already started : $pid";
78                         return 0;
79                 fi;
80                 echo "Starting $item";
81         fi;
82
83         if [ $action == "stop" ]; then
84
85                 if [ ! -e $pidfile ]; then
86                         echo "$item not running";
87                         return 0;
88                 fi;
89
90                 pid=$(cat $pidfile);
91                 echo "Stopping $item : $pid";
92                 kill -s INT $pid;
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_C_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_PERL_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
122         pid=$!;
123         echo $pid > $PID_OSRF_PERL;
124         sleep 5;
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         do_action "start" $PID_OSRF_C "OpenSRF C";
136         opensrf-c $(hostname -f) $OPT_C_CONFIG opensrf;
137         pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
138         echo $pid > "$PID_OSRF_C";
139         return 0;
140 }
141
142 function stop_c {
143         do_action "stop" $PID_OSRF_C "OpenSRF C";
144         killall -9 opensrf-c  # hack for now to force kill all C services
145         sleep 1;
146         return 0;
147 }
148
149
150
151 # ---------------------------------------------------------------------------
152 # Do the requested action
153 # ---------------------------------------------------------------------------
154 case $OPT_ACTION in
155         "start_router") start_router;;
156         "stop_router") stop_router;;
157         "restart_router") stop_router; start_router;;
158         "start_perl") start_perl;;
159         "stop_perl") stop_perl;;
160         "restart_perl") stop_perl; start_perl;;
161         "start_c") start_c;;
162         "stop_c") stop_c;;
163         "restart_c") stop_c; start_c;;
164         "start_osrf") start_perl; start_c;;
165         "stop_osrf") stop_perl; stop_c;;
166         "restart_osrf") stop_perl; stop_c; start_perl; start_c;;
167         "stop_all") stop_c; stop_perl; stop_router;;
168         "start_all") start_router; start_perl; start_c;;
169         "restart_all") stop_c; stop_perl; stop_router; start_router; start_perl; start_c;;
170         *) usage;;
171 esac;
172
173
174