-
Notifications
You must be signed in to change notification settings - Fork 8
/
uwsgi.init
executable file
·156 lines (137 loc) · 2.95 KB
/
uwsgi.init
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
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi app server
# Description: starts uwsgi app server using start-stop-daemon
### END INIT INFO
set -e
VERSION=$(basename $0)
if [[ "uwsgi uwsgi.init" =~ $VERSION ]]; then
echo "Please use version-specific uwsgi!"
find /etc/init.d/ -name 'uwsgi-2.?' -exec echo " {}" \;
exit 1
fi
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/$VERSION
RUN=/var/run/$VERSION/
CONFIGS_DIR=/etc/uwsgi/$VERSION/sites-enabled
OWNER=www-data
NAME=$VERSION
DESC=$VERSION
OP=$1
if [[ -n $2 ]]; then
shift
ENABLED_CONFIGS=""
for c in $@; do
[[ -f $CONFIGS_DIR/$c ]] && \
ENABLED_CONFIGS=$(echo -e "$ENABLED_CONFIGS\n$CONFIGS_DIR/$c")
done
else
ENABLED_CONFIGS=$(find $CONFIGS_DIR/ -not -type d)
fi
[[ -x $DAEMON ]] || exit 0
[[ -d $RUN ]] || mkdir $RUN && chown www-data $RUN
DAEMON_OPTS=""
# Include uwsgi defaults if available
if [[ -f /etc/default/$VERSION ]]; then
. /etc/default/$VERSION
fi
do_pid_check()
{
local PIDFILE=$1
[[ -f $PIDFILE ]] || return 0
local PID=$(cat $PIDFILE)
for p in $(pgrep $VERSION); do
[[ $p == $PID ]] && return 1
done
return 0
}
do_start()
{
for config in $ENABLED_CONFIGS; do
local PIDFILE=$RUN/$(basename $config).pid
if do_pid_check $PIDFILE; then
sudo -u $OWNER -i $VERSION $config $DAEMON_OPTS --pidfile $PIDFILE
else
echo "Already running!"
fi
done
}
send_sig()
{
for config in $ENABLED_CONFIGS; do
local PIDFILE=$RUN/$(basename $config).pid
set +e
[[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
set -e
done
}
wait_and_clean_pidfiles()
{
for config in $ENABLED_CONFIGS; do
local PIDFILE=$RUN/$(basename $config).pid
until do_pid_check $PIDFILE; do
echo -n "";
done
rm -f $PIDFILE
done
}
do_stop()
{
send_sig -3
wait_and_clean_pidfiles
}
do_reload()
{
send_sig -1
}
do_force_reload()
{
send_sig -15
}
get_status()
{
send_sig -10
}
case "$OP" in
start)
echo "Starting $DESC: "
do_start
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
do_stop
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC: "
do_reload
echo "$NAME."
;;
force-reload)
echo -n "Force-reloading $DESC: "
do_force_reload
echo "$NAME."
;;
restart)
echo "Restarting $DESC: "
do_stop
sleep 1
do_start
echo "$NAME."
;;
status)
get_status
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
exit 1
;;
esac
exit 0