Script for starting, stoping, restarting django fastcgi daemons

 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
#!/bin/sh
# This file should be included after defining following variables:
# SITE_DIR - directory which should be active before starting daemons
# SITE - site ID which will be used in generating pid and sock files
if [ -z "$SITE_DIR" ]; then
echo "SITE_DIR is not defined"
exit 1
fi
if [ -z "$SITE" ]; then
echo "SITE is not defined"
exit 1
fi
PYTHON=/usr/local/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 maxchildren=1000"
#COMMAND="$PYTHON ./manage.py runfcgi maxchildren=3 maxrequest=1000 minspare=1 maxspare=1 socket=$SOCKFILE pidfile=$PIDFILE"
usage() {
echo "$0 {start|stop|restart}"
exit 1
}
daemon_command() {
echo -n $COMMAND
}
daemon_pids() {
echo $(ps axwwo pid,command|grep "$(daemon_command)"|grep -v grep|cut -c 1-6)
}
start() {
PIDS=$(daemon_pids)
if [ -n "$PIDS" ]; then
echo "daemons already running"
else
echo "Loading daemons"
if [ ! -e $SITE_DIR ]; then
echo "$SITE_DIR does not exist"
else
cd $SITE_DIR
echo $(daemon_command)
$(daemon_command)
fi
fi
}
stop() {
PIDS=$(daemon_pids)
if [ -n "$PIDS" ]; then
echo "Killing old daemons"
for pid in $PIDS; do
kill $pid
done
else
echo "No daemons running"
fi
}
pids() {
echo "pids: $(daemon_pids)"
}
restart() {
stop
sleep 2
start
}
action=$1
shift
case $action in
"start") start;;
"stop") stop;;
"pids") pids;;
"restart") restart;;
*) echo $(usage);;
esac