-
Notifications
You must be signed in to change notification settings - Fork 4
/
serve.sh
executable file
·135 lines (111 loc) · 3.17 KB
/
serve.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
#!/bin/bash
###############################################################################
###############################################################################
HOSTDEF="127.0.0.1"
PORTDEF="3001"
###############################################################################
###############################################################################
function hostval() {
local hostval=${1/:*/}
if [ -z "${hostval}" ]
then
echo $2 ## default
else
echo ${hostval}
fi
}
function portval() {
local hostval=${1/:*/}
local portval=${1/*:/}
if [ -z "${portval}" ]
then
echo $2 ## default
else
echo ${portval/$hostval/$2}
fi
}
###############################################################################
###############################################################################
ACTMETH=${1-restart}
HOSTVAL=$(hostval ${2-$HOSTDEF:$PORTDEF} $HOSTDEF)
PORTVAL=$(portval ${2-$HOSTDEF:$PORTDEF} $PORTDEF)
CGIPROT=${3-fcgi}
SRVMETH=${4-threaded}
DAEMOON=${5-true}
PROJDIR=$(pwd)
PIDFILE=$PROJDIR/.pid-$PORTVAL
###############################################################################
###############################################################################
source secret.sh
###############################################################################
###############################################################################
function start() {
if [ -f $1 ]; then
local pid=$(cat -- $1)
local sta=$(ps $pid)
if [ "$?" -eq "0" ] ; then
exit 0
fi
rm -f -- $1
fi
if [ -f bin/activate ] ; then
source bin/activate
fi
exec ./manage.py runfcgi \
method=$SRVMETH protocol=$CGIPROT \
host=$HOSTVAL port=$PORTVAL \
pidfile=$1 daemonize=$DAEMOON
}
function stop() {
if [ -f $1 ]; then
kill $(cat -- $1) &> /dev/null
rm -f -- $1
fi
}
function status() {
if [ -f $1 ]; then
PID=$(cat -- $1)
STA=$(ps $PID)
if [ "$?" -eq "0" ] ; then
echo "[$1] live: $PID"
else
echo "[$1] dead: $PID"
fi
else
echo "[$1] dead"
fi
}
###############################################################################
###############################################################################
case $ACTMETH in
restart)
$0 stop $2 $3 $4 $5
$0 start $2 $3 $4 $5
;;
start)
start $PIDFILE
;;
stop)
stop $PIDFILE
;;
status)
status $PIDFILE
;;
stop-all)
for PIF in $(ls $PROJDIR/.pid-* 2> /dev/null) ; do
stop $PIF
done
;;
status-all)
for PIF in $(ls $PROJDIR/.pid-* 2> /dev/null) ; do
status $PIF
done
;;
*)
$0 restart
esac
###############################################################################
###############################################################################
exit 0
###############################################################################
###############################################################################