1
|
#! /bin/bash
|
2
|
### BEGIN INIT INFO
|
3
|
# Provides: calebasse
|
4
|
# Required-Start: $remote_fs $syslog
|
5
|
# Required-Stop: $remote_fs $syslog
|
6
|
# Default-Start: 2 3 4 5
|
7
|
# Default-Stop: 0 1 6
|
8
|
# Short-Description: Calebasse
|
9
|
# Description: Calebasse
|
10
|
### END INIT INFO
|
11
|
|
12
|
# Author: Jérôme Schneider <jschneider@entrouvert.com>
|
13
|
|
14
|
# Do NOT "set -e"
|
15
|
|
16
|
# PATH should only include /usr/* if it runs after the mountnfs.sh script
|
17
|
PATH=/sbin:/usr/sbin:/bin:/usr/bin
|
18
|
DESC="Description of the service"
|
19
|
NAME=calebasse
|
20
|
HOME=/home/calebasse/venv
|
21
|
DAEMON=$HOME/bin/gunicorn
|
22
|
PID_DIR=/var/run/$NAME
|
23
|
PIDFILE=$PID_DIR/gunicorn.pid
|
24
|
# set timeout to 10800 seconds, as apache2 mod_proxy ProxyTimeout default value
|
25
|
# put error log into /var/log/calebasse/gunicorn.error.log
|
26
|
# put access log into /var/log/calebasse/gunicorn.access.log
|
27
|
# use 4 workers
|
28
|
DAEMON_ARGS="$NAME.wsgi:application -D -p $PIDFILE \
|
29
|
-t 10800 \
|
30
|
--bind=unix:$PID_DIR/calebasse.sock \
|
31
|
--error-logfile /var/log/$NAME/gunicorn.error.log \
|
32
|
--access-logfile /var/log/$NAME/gunicorn.access.log \
|
33
|
-w 4"
|
34
|
SCRIPTNAME=/etc/init.d/$NAME
|
35
|
START_STOP_OPTIONS="--chuid $NAME --group $NAME"
|
36
|
export LANG=fr_FR.UTF-8
|
37
|
|
38
|
if [ -f /etc/gunicorn/$NAME-conf.py ]; then
|
39
|
DAEMON_ARGS="$DAEMON_ARGS -c $HOME/calebasse/$NAME-conf.py"
|
40
|
fi
|
41
|
|
42
|
# Exit if the package is not installed
|
43
|
[ -x "$DAEMON" ] || exit 0
|
44
|
|
45
|
# Read configuration variable file if it is present
|
46
|
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
|
47
|
|
48
|
# Define LSB log_* functions.
|
49
|
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
|
50
|
# and status_of_proc is working.
|
51
|
. /lib/lsb/init-functions
|
52
|
|
53
|
# Loading venv
|
54
|
source $HOME/bin/activate
|
55
|
cd $HOME/calebasse
|
56
|
|
57
|
#
|
58
|
# Function that starts the daemon/service
|
59
|
#
|
60
|
do_start()
|
61
|
{
|
62
|
# Return
|
63
|
# 0 if daemon has been started
|
64
|
# 1 if daemon was already running
|
65
|
# 2 if daemon could not be started
|
66
|
start-stop-daemon --start --quiet $START_STOP_OPTIONS --exec $DAEMON --test > /dev/null \
|
67
|
|| return 1
|
68
|
start-stop-daemon --start --quiet $START_STOP_OPTIONS --exec $DAEMON -- \
|
69
|
$DAEMON_ARGS \
|
70
|
|| return 2
|
71
|
# Add code here, if necessary, that waits for the process to be ready
|
72
|
# to handle requests from services started subsequently which depend
|
73
|
# on this one. As a last resort, sleep for some time.
|
74
|
}
|
75
|
|
76
|
#
|
77
|
# Function that stops the daemon/service
|
78
|
#
|
79
|
do_stop()
|
80
|
{
|
81
|
# Return
|
82
|
# 0 if daemon has been stopped
|
83
|
# 1 if daemon was already stopped
|
84
|
# 2 if daemon could not be stopped
|
85
|
# other if a failure occurred
|
86
|
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
|
87
|
RETVAL="$?"
|
88
|
[ "$RETVAL" = 2 ] && return 2
|
89
|
# Wait for children to finish too if this is a daemon that forks
|
90
|
# and if the daemon is only ever run from this initscript.
|
91
|
# If the above conditions are not satisfied then add some other code
|
92
|
# that waits for the process to drop all resources that could be
|
93
|
# needed by services started subsequently. A last resort is to
|
94
|
# sleep for some time.
|
95
|
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
|
96
|
[ "$?" = 2 ] && return 2
|
97
|
# Many daemons don't delete their pidfiles when they exit.
|
98
|
rm -f $PIDFILE
|
99
|
return "$RETVAL"
|
100
|
}
|
101
|
|
102
|
#
|
103
|
# Function that sends a SIGHUP to the daemon/service
|
104
|
#
|
105
|
do_reload() {
|
106
|
#
|
107
|
# If the daemon can reload its configuration without
|
108
|
# restarting (for example, when it is sent a SIGHUP),
|
109
|
# then implement that here.
|
110
|
#
|
111
|
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE
|
112
|
return 0
|
113
|
}
|
114
|
|
115
|
case "$1" in
|
116
|
start)
|
117
|
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
|
118
|
do_start
|
119
|
case "$?" in
|
120
|
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
|
121
|
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
|
122
|
esac
|
123
|
;;
|
124
|
stop)
|
125
|
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
|
126
|
do_stop
|
127
|
case "$?" in
|
128
|
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
|
129
|
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
|
130
|
esac
|
131
|
;;
|
132
|
status)
|
133
|
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
|
134
|
;;
|
135
|
reload|force-reload)
|
136
|
#
|
137
|
# If do_reload() is not implemented then leave this commented out
|
138
|
# and leave 'force-reload' as an alias for 'restart'.
|
139
|
#
|
140
|
log_daemon_msg "Reloading $DESC" "$NAME"
|
141
|
do_reload
|
142
|
log_end_msg $?
|
143
|
;;
|
144
|
restart|force-reload)
|
145
|
#
|
146
|
# If the "reload" option is implemented then remove the
|
147
|
# 'force-reload' alias
|
148
|
#
|
149
|
log_daemon_msg "Restarting $DESC" "$NAME"
|
150
|
do_stop
|
151
|
case "$?" in
|
152
|
0|1)
|
153
|
do_start
|
154
|
case "$?" in
|
155
|
0) log_end_msg 0 ;;
|
156
|
1) log_end_msg 1 ;; # Old process is still running
|
157
|
*) log_end_msg 1 ;; # Failed to start
|
158
|
esac
|
159
|
;;
|
160
|
*)
|
161
|
# Failed to stop
|
162
|
log_end_msg 1
|
163
|
;;
|
164
|
esac
|
165
|
;;
|
166
|
*)
|
167
|
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
|
168
|
exit 3
|
169
|
;;
|
170
|
esac
|
171
|
|
172
|
:
|