-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrapidsms-init.sh
executable file
·159 lines (136 loc) · 3.98 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
#!/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 'manage.py' 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: amatd 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="iraqyouth" # change to your project name
DAEMON=$WHERE_AM_I/manage.py
DAEMON_OPTS=""
RUN_AS=root
APP_PATH=$WHERE_AM_I
ROUTER_PID_FILE=/var/run/${NAME}_router.pid
#WEBSERVER_PID_FILE=/var/run/${NAME}_webs.pid
WEBSERVER_PORT=8801
WEBSERVER_IP=127.0.0.1
OUT_LOG=/var/log/shadowpoll/shadow_poll_out
ERR_LOG=/var/log/shadowpoll/shadow_poll_err
############### END EDIT ME ##################
test -x $DAEMON || exit 0
do_start() {
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
echo -n "Starting webserver for $NAME... "
start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --exec $DAEMON -- runfcgi -v 2 host=$WEBSERVER_IP port=$WEBSERVER_PORT outlog=$OUT_LOG errlog=$ERR_LOG
echo "Webserver Started"
}
hard_stop_runserver() {
for i in `ps aux | grep -i "manage.py runfcgi" | grep -v grep | awk '{print $2}' ` ; do
kill -9 $i
done
echo "Hard stopped runserver"
}
hard_stop_spomsky() {
for i in `ps aux | grep -i "spomskyd" | grep -v grep | awk '{print $2}' ` ; do
kill -9 $i
done
rm $SPOMSKY_PID_FILE 2>/dev/null
echo "Hard stopped Spomskyd"
}
hard_stop_router() {
for i in `ps aux | grep -i "manage.py route" | grep -v grep | awk '{print $2}' ` ; do
kill -9 $i
done
rm $ROUTER_PID_FILE 2>/dev/null
echo "Hard stopped router"
}
do_hard_restart() {
do_hard_stop_all
do_start
}
do_hard_stop_all() {
hard_stop_runserver
hard_stop_router
}
do_stop() {
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
echo -n "Stopping webserver for $NAME... "
hard_stop_runserver
echo "Webserver Stopped"
}
do_restart() {
do_stop
sleep 2
do_start
}
# check on PID's, if not running, restart
do_check_restart() {
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
# now check for runserver
webs=`ps aux | grep -i "manage.py runserver" | grep -v grep | wc -l`
if [ $webs -lt 2 ] ; then
echo "Can't find webserver, doing hard stop, restart"
do_hard_restart
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