bin bash PYTHON usr bin python PIDFILE var run www SITE pid SOCKFILE v

 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
#!/bin/bash
PYTHON=/usr/bin/python
PIDFILE=/var/run/www/@SITE.pid
SOCKFILE=/var/run/www/@SITE.sock
#COMMAND="$PYTHON ./manage.py runfcgi method=threaded socket=@SOCKFILE pidfile=@PIDFILE"
COMMAND="$PYTHON ./manage.py runfcgi maxchildren=3 maxrequest=1000 minspare=1 maxspare=1 socket=@SOCKFILE pidfile=@PIDFILE"
usage() {
echo "$0 {start|stop|restart} domain[domain]*"
exit 1
}
daemon_command() {
SITE_SOCKFILE=${SOCKFILE/@SITE/$1}
SITE_PIDFILE=${PIDFILE/@SITE/$1}
COMMAND=${COMMAND/@SOCKFILE/$SITE_SOCKFILE}
COMMAND=${COMMAND/@PIDFILE/$SITE_PIDFILE}
echo -n $COMMAND
}
daemon_pids() {
echo $(ps axwwo pid,command|grep "$(daemon_command $1)"|grep -v grep|cut -c 1-6)
}
start() {
if [ -z "$1" ]; then usage; fi
for SITE in $1; do
PIDS=$(daemon_pids $SITE)
if [ -n "$PIDS" ]; then
echo "$SITE daemon already running"
else
echo "Loading $SITE daemon"
DIR=/web/$SITE/web_site
if [ ! -e $DIR ]; then
echo "$DIR does not exist"
else
cd $DIR
$(daemon_command $SITE)
fi
fi
done
}
stop() {
if [ -z "$1" ]; then usage; fi
for SITE in $1; do
PIDS=$(daemon_pids $SITE)
if [ -n "$PIDS" ]; then
echo "Killing old $SITE daemons"
for pid in $PIDS; do
kill $pid
done
else
echo "No $SITE daemons running"
fi
done
}
pids() {
if [ -z "$1" ]; then usage; fi
for SITE in $1; do
echo "$SITE pids: $(daemon_pids $SITE)"
done
}
restart() {
if [ -z "$1" ]; then usage; fi
for SITE in $1; do
stop $1
sleep 2
start $1
done
}
action=$1
shift
case $action in
"start") start "$*";;
"stop") stop "$*";;
"pids") pids "$*";;
"restart") restart "$*";;
*) echo $(usage);;
esac