-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrapidsms-init.sh
185 lines (160 loc) · 4.89 KB
/
rapidsms-init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/sh
#
# IMPORTANT: To use, do the folling:
#
# 1. Change 'NAME' variable to the name of your project. E.g. "bednets_for_nigeria"
# 2. Place this file in the TOP-LEVEL of your project, right where 'rapidsms' is
# 3. Link it into /etc/init.d e.g. > ln -s /usr/local/my_project/rapidsms-init.sh /etc/init.d/
# 4. Add it to the runlevels, on Ubuntu/Debian there is a nice tool to do this for you:
# > sudo update-rc.d rapidsms-init.sh defaults
#
# NOTE: If you want to run multiple instances of RapidSMS, just put this init file in each project dir,
# set a different NAME for each project, link it into /etc/init.d with _different_ names,
# and add _each_ script to the runlevels.
#
### BEGIN INIT INFO
# Provides: rapidsms daemon instance
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts instances of rapidsms router and web server
# Description: starts instance of rapidsms router and web server using start-stop-daemon
### END INIT INFO
# set -e
ME=`readlink -f $0`
WHERE_AM_I=`dirname $ME`
############### EDIT ME ##################
NAME="ubuzima" # change to your project name
DAEMON=$WHERE_AM_I/
DAEMON_OPTS=""
RUN_AS=
APP_PATH=$WHERE_AM_I
ROUTER_PID_FILE=//${NAME}_router.pid
#WEBSERVER_PID_FILE=/var/run/${NAME}_webs.pid
WEBSERVER_PORT=
#WEBSERVER_IP=
WEBSERVER_IP=
# By default both router and webserver are started
# You may turn off one or the other by setting the appropraite
# value to 0 for off or 1 for on.
#
# Most common use case would be to turn off the webserver
# because you are running Django in another container like Apache
# Nginx, or CherryPy
#
START_ROUTER=1
START_WEBSERVER=1
############### END EDIT ME ##################
test -x $DAEMON || exit 0
do_start() {
if [ "${START_ROUTER}" -eq 1 ] ; then
echo -n "Starting router for $NAME... "
start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $ROUTER_PID_FILE --make-pidfile --exec $DAEMON -- route $DAEMON_OPTS
echo "Router Started"
sleep 2
fi
if [ "${START_WEBSERVER}" -eq 1 ] ; then
echo -n "Starting webserver and FCGI processes for $NAME... "
#start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --exec $DAEMON -- runserver $WEBSERVER_IP:$WEBSERVER_PORT
#start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --exec $DAEMON -- runfcgi protocol=scgi host=$WEBSERVER_IP port=$WEBSERVER_PORT
#the process of the running app || echo Failed to start.
echo "Webserver and FCGI processes started"
fi
}
hard_stop_runserver() {
echo "Hard stopping runserver"
for i in `ps aux | grep -i "rapidsms runserver" | grep -v grep | awk '{print $2}' ` ; do
kill -9 $i
done
}
hard_stop_runfcgi() {
echo "Hard stopping runfcgi"
for i in `ps aux | grep -i "rapidsms runfcgi" | grep -v grep | awk '{print $2}' ` ; do
kill -9 $i
done
}
hard_stop_router() {
echo "Hard stopping router"
for i in `ps aux | grep -i "rapidsms route" | grep -v grep | awk '{print $2}' ` ; do
kill -9 $i
done
rm $ROUTER_PID_FILE 2>/dev/null
}
do_hard_restart() {
do_hard_stop_all
do_start
}
do_hard_stop_all() {
hard_stop_runserver
hard_stop_runfcgi
hard_stop_router
}
do_stop() {
if [ "${START_ROUTER}" -eq 1 ] ; then
echo -n "Stopping router for $NAME... "
start-stop-daemon --stop --pidfile $ROUTER_PID_FILE
rm $ROUTER_PID_FILE 2>/dev/null
echo "Router Stopped"
sleep 2
fi
if [ "${START_WEBSERVER}" -eq 1 ] ; then
echo -n "Stopping webserver and FCGI processes for $NAME... "
hard_stop_runserver
hard_stop_runfcgi
echo "Webserver and FCGI processes stopped"
fi
}
do_restart() {
do_stop
sleep 2
do_start
}
# check on PID's, if not running, restart
do_check_restart() {
if [ "${START_ROUTER}" -eq 1 ] ; then
for pidf in $ROUTER_PID_FILE ; do
if [ -f $pidf ] ; then
pid=`cat $pidf`
if [ ! -e /proc/$pid ] ; then
echo "Process for file $pidf not running. Performing hard stop, restart"
do_hard_restart
return
fi
fi
done
fi
# now check for runserver
if [ "${START_WEBSERVER}" -eq 1 ] ; then
webs=`ps aux | grep -i "rapidsms runserver" | grep -v grep | wc -l`
if [ $webs -lt 2 ] ; then
echo "Can't find webserver, doing hard stop, restart"
do_hard_restart
fi
fi
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
check-restart)
do_check_restart
;;
hard-stop)
do_hard_stop_all
;;
hard-restart)
do_hard_restart
;;
restart|force-reload)
do_restart
;;
*)
echo "Usage: $ME {start|stop|restart|force-reload|check-restart|hard-stop|hard-restart}" >&2
exit 1
;;
esac
exit 0