| 1 | #! /bin/sh |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: FastCGI servers for Django |
| 4 | # Required-Start: networking |
| 5 | # Required-Stop: networking |
| 6 | # Default-Start: 2 3 4 5 |
| 7 | # Default-Stop: S 0 1 6 |
| 8 | # Short-Description: Start FastCGI servers with Django. |
| 9 | # Description: Django, in order to operate with FastCGI, must be started |
| 10 | # in a very specific way with manage.py. This must be done |
| 11 | # for each Django web server that has to run. |
| 12 | ### END INIT INFO |
| 13 | # |
| 14 | # Author: Guillermo Fernandez Castellanos |
| 15 | # <guillermo.fernandez.castellanos AT gmail.com>. |
| 16 | # |
| 17 | # Changed: Jannis Leidel |
| 18 | # <jannis AT leidel.info> |
| 19 | # Joost Cassee |
| 20 | # <joost@cassee.net> |
| 21 | # Sebastian Rahlf |
| 22 | # <basti AT redtoad.de> |
| 23 | # |
| 24 | # Version: @(#)fastcgi 0.4 28-Jul-2009 basti AT redtoad.de |
| 25 | # |
| 26 | |
| 27 | set -e |
| 28 | |
| 29 | #### CONFIGURATION (override in /etc/default/django) |
| 30 | |
| 31 | # django project names/directories |
| 32 | DJANGO_SITES="" |
| 33 | |
| 34 | # path to the directory with your django projects |
| 35 | SITES_PATH=/var/lib/django |
| 36 | |
| 37 | # path to the directory for socket and pid files |
| 38 | RUNFILES_PATH=/var/run/django |
| 39 | |
| 40 | # please make sure this is NOT root |
| 41 | # local user prefered, www-data accepted |
| 42 | RUN_AS=www-data |
| 43 | |
| 44 | # maximum requests before fast-cgi process respawns |
| 45 | # (a.k.a. get killed and let live) |
| 46 | MAXREQUESTS=1000 |
| 47 | |
| 48 | #### END CONFIGURATION |
| 49 | |
| 50 | # Include defaults if available |
| 51 | if [ -f /etc/default/django ] ; then |
| 52 | . /etc/default/django |
| 53 | fi |
| 54 | |
| 55 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
| 56 | DESC="Django FastCGI servers" |
| 57 | NAME=$0 |
| 58 | SCRIPTNAME=/etc/init.d/$NAME |
| 59 | mkdir -p $RUNFILES_PATH |
| 60 | chown -R $RUN_AS:$RUN_AS $RUNFILES_PATH |
| 61 | |
| 62 | # A specific site can be started/stopped by appending its name |
| 63 | SITE=$2 |
| 64 | if [ -n "$SITE" ]; then |
| 65 | DJANGO_SITES=$SITE |
| 66 | fi |
| 67 | |
| 68 | # |
| 69 | # Function that starts the daemon/service. |
| 70 | # |
| 71 | d_start() |
| 72 | { |
| 73 | # Starting all Django FastCGI processes |
| 74 | # PORT=$PORT_START |
| 75 | for SITE in $DJANGO_SITES |
| 76 | do |
| 77 | echo -n " $SITE" |
| 78 | if [ -f $RUNFILES_PATH/$SITE.pid ]; then |
| 79 | echo -n " already running" |
| 80 | else |
| 81 | start-stop-daemon --start --quiet \ |
| 82 | --pidfile $RUNFILES_PATH/$SITE.pid \ |
| 83 | --chuid $RUN_AS --exec /usr/bin/env -- python \ |
| 84 | $SITES_PATH/$SITE/manage.py runfcgi \ |
| 85 | protocol=fcgi method=threaded \ |
| 86 | maxrequests=$MAXREQUESTS \ |
| 87 | pidfile=$RUNFILES_PATH/$SITE.pid \ |
| 88 | socket=$RUNFILES_PATH/$SITE.socket |
| 89 | chmod 400 $RUNFILES_PATH/$SITE.pid |
| 90 | fi |
| 91 | sleep 1 |
| 92 | done |
| 93 | } |
| 94 | |
| 95 | # |
| 96 | # Function that stops the daemon/service. |
| 97 | # |
| 98 | d_stop() { |
| 99 | # Killing all Django FastCGI processes running |
| 100 | for SITE in $DJANGO_SITES |
| 101 | do |
| 102 | echo -n " $SITE" |
| 103 | start-stop-daemon --stop --quiet --pidfile $RUNFILES_PATH/$SITE.pid \ |
| 104 | || echo -n " not running" |
| 105 | if [ -f $RUNFILES_PATH/$SITE.pid ]; then |
| 106 | rm -f $RUNFILES_PATH/$SITE.pid |
| 107 | fi |
| 108 | sleep 1 |
| 109 | done |
| 110 | } |
| 111 | |
| 112 | ACTION="$1" |
| 113 | case "$ACTION" in |
| 114 | start) |
| 115 | echo -n "Starting $DESC:" |
| 116 | d_start |
| 117 | echo "." |
| 118 | ;; |
| 119 | |
| 120 | stop) |
| 121 | echo -n "Stopping $DESC:" |
| 122 | d_stop |
| 123 | echo "." |
| 124 | ;; |
| 125 | |
| 126 | status) |
| 127 | echo "Status of $DESC:" |
| 128 | for SITE in $DJANGO_SITES |
| 129 | do |
| 130 | echo -n " $SITE" |
| 131 | if [ -f $RUNFILES_PATH/$SITE.pid ]; then |
| 132 | echo " running ($(cat $RUNFILES_PATH/$SITE.pid))" |
| 133 | else |
| 134 | echo " not running" |
| 135 | fi |
| 136 | done |
| 137 | ;; |
| 138 | |
| 139 | restart|force-reload) |
| 140 | echo -n "Restarting $DESC: $NAME" |
| 141 | d_stop |
| 142 | sleep 2 |
| 143 | d_start |
| 144 | echo "." |
| 145 | ;; |
| 146 | |
| 147 | *) |
| 148 | echo "Usage: $NAME {start|stop|restart|force-reload|status} [site]" >&2 |
| 149 | exit 3 |
| 150 | ;; |
| 151 | esac |
| 152 | |
| 153 | exit 0 |