| 1 | #!/bin/sh
|
|---|
| 2 | ### BEGIN INIT INFO
|
|---|
| 3 | # Provides: uwsgi-pootle
|
|---|
| 4 | # Required-Start: $local_fs $remote_fs $network
|
|---|
| 5 | # Required-Stop: $local_fs $remote_fs $network
|
|---|
| 6 | # Default-Start: 2 3 4 5
|
|---|
| 7 | # Default-Stop: 0 1 6
|
|---|
| 8 | # Short-Description: Start/stop custom uWSGI server instance
|
|---|
| 9 | ### END INIT INFO
|
|---|
| 10 |
|
|---|
| 11 | # Author: Leonid Borisenko <leo.borisenko@gmail.com>
|
|---|
| 12 | # modified for Pootle by: Fran Boon <fran@aidiq.com>
|
|---|
| 13 |
|
|---|
| 14 | # PATH should only include /usr/* if it runs after the mountnfs.sh script
|
|---|
| 15 | PATH=/sbin:/usr/sbin:/bin:/usr/bin
|
|---|
| 16 |
|
|---|
| 17 | NAME="pootle" # Introduce the short server's name here
|
|---|
| 18 | SCRIPTNAME=/etc/init.d/uwsgi-$NAME
|
|---|
| 19 |
|
|---|
| 20 | DESC="Pootle uwsgi server" # Introduce a short description here
|
|---|
| 21 | DAEMON=/usr/local/bin/uwsgi # Introduce the server's location here
|
|---|
| 22 |
|
|---|
| 23 | UWSGI_UID=pootle # Introduce uWSGI uid here
|
|---|
| 24 | UWSGI_GID=pootle # Introduve uWSGI gid here
|
|---|
| 25 |
|
|---|
| 26 | UWSGI_RUNDIR=/run/uwsgi
|
|---|
| 27 | UWSGI_SPECIFIC_RUNDIR="$UWSGI_RUNDIR/$NAME"
|
|---|
| 28 |
|
|---|
| 29 | PIDFILE=$UWSGI_SPECIFIC_RUNDIR/pid
|
|---|
| 30 |
|
|---|
| 31 | DAEMON_ARGS=" \
|
|---|
| 32 | --daemonize /var/log/uwsgi/${NAME}.log \
|
|---|
| 33 | --pidfile $PIDFILE \
|
|---|
| 34 | --uid $UWSGI_UID \
|
|---|
| 35 | --gid $UWSGI_GID \
|
|---|
| 36 | --ini /home/pootle/uwsgi.ini \
|
|---|
| 37 | "
|
|---|
| 38 |
|
|---|
| 39 | # Exit if the package is not installed
|
|---|
| 40 | [ -x $DAEMON ] || exit 0
|
|---|
| 41 |
|
|---|
| 42 | # Load the VERBOSE setting and other rcS variables
|
|---|
| 43 | . /lib/init/vars.sh
|
|---|
| 44 |
|
|---|
| 45 | # Define LSB log_* functions.
|
|---|
| 46 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
|
|---|
| 47 | . /lib/lsb/init-functions
|
|---|
| 48 |
|
|---|
| 49 | # Return:
|
|---|
| 50 | # 0 if daemon has been started
|
|---|
| 51 | # 1 if daemon was already running
|
|---|
| 52 | # 2 if daemon could not be started
|
|---|
| 53 | do_start()
|
|---|
| 54 | {
|
|---|
| 55 | install -d -o root -g root -m 755 $UWSGI_RUNDIR
|
|---|
| 56 | install -d -o $UWSGI_UID -g $UWSGI_GID -m 755 $UWSGI_SPECIFIC_RUNDIR
|
|---|
| 57 |
|
|---|
| 58 | start-stop-daemon --start --quiet \
|
|---|
| 59 | --pidfile $PIDFILE \
|
|---|
| 60 | --exec $DAEMON \
|
|---|
| 61 | --test > /dev/null \
|
|---|
| 62 | || return 1
|
|---|
| 63 |
|
|---|
| 64 | start-stop-daemon --start --quiet \
|
|---|
| 65 | --pidfile $PIDFILE \
|
|---|
| 66 | --exec $DAEMON -- $DAEMON_ARGS \
|
|---|
| 67 | || return 2
|
|---|
| 68 |
|
|---|
| 69 | local INTERVAL_START=$(date +%s)
|
|---|
| 70 | local INTERVAL_END=$(date +%s)
|
|---|
| 71 | local WAITING=2 # seconds
|
|---|
| 72 |
|
|---|
| 73 | # Wait until daemon getting to create pidfile.
|
|---|
| 74 | while [ ! -e "$PIDFILE" ]; do
|
|---|
| 75 | INTERVAL_END=$(date +%s)
|
|---|
| 76 | if [ $(expr $INTERVAL_END - $INTERVAL_START) -gt $WAITING ]; then
|
|---|
| 77 | return
|
|---|
| 78 | fi
|
|---|
| 79 | sleep 0.05
|
|---|
| 80 | done
|
|---|
| 81 |
|
|---|
| 82 | chown root:root $PIDFILE
|
|---|
| 83 | chmod 644 $PIDFILE
|
|---|
| 84 |
|
|---|
| 85 | return 0
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | # Return:
|
|---|
| 89 | # 0 if daemon has been stopped
|
|---|
| 90 | # 1 if daemon was already stopped
|
|---|
| 91 | # 2 if daemon could not be stopped
|
|---|
| 92 | # other if a failure occurred
|
|---|
| 93 | do_stop()
|
|---|
| 94 | {
|
|---|
| 95 | start-stop-daemon --stop --quiet \
|
|---|
| 96 | --retry=QUIT/30/KILL/5 \
|
|---|
| 97 | --pidfile $PIDFILE \
|
|---|
| 98 | --exec $DAEMON
|
|---|
| 99 |
|
|---|
| 100 | RETVAL="$?"
|
|---|
| 101 | [ "$RETVAL" = 2 ] && return 2
|
|---|
| 102 |
|
|---|
| 103 | rm -rf $RUNDIR
|
|---|
| 104 |
|
|---|
| 105 | return "$RETVAL"
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | # Return:
|
|---|
| 109 | # 0 if daemon has been reloaded
|
|---|
| 110 | # 3 if daemon could not be reloaded
|
|---|
| 111 | do_reload()
|
|---|
| 112 | {
|
|---|
| 113 | start-stop-daemon --stop --quiet \
|
|---|
| 114 | --signal=HUP \
|
|---|
| 115 | --pidfile $PIDFILE \
|
|---|
| 116 | --exec $DAEMON
|
|---|
| 117 |
|
|---|
| 118 | RETVAL="$?"
|
|---|
| 119 |
|
|---|
| 120 | # There is no such process, nothing to reload!
|
|---|
| 121 | [ "$RETVAL" = 1 ] && RETVAL=3
|
|---|
| 122 |
|
|---|
| 123 | return "$RETVAL"
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | # Return:
|
|---|
| 127 | # 0 if daemon has been reloaded
|
|---|
| 128 | # 3 if daemon could not be reloaded
|
|---|
| 129 | do_force_reload()
|
|---|
| 130 | {
|
|---|
| 131 | start-stop-daemon --stop --quiet \
|
|---|
| 132 | --signal=TERM \
|
|---|
| 133 | --pidfile $PIDFILE \
|
|---|
| 134 | --exec $DAEMON
|
|---|
| 135 |
|
|---|
| 136 | RETVAL="$?"
|
|---|
| 137 |
|
|---|
| 138 | # There is no such process, nothing to reload!
|
|---|
| 139 | [ "$RETVAL" = 1 ] && RETVAL=3
|
|---|
| 140 |
|
|---|
| 141 | return "$RETVAL"
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | case "$1" in
|
|---|
| 145 | start)
|
|---|
| 146 | log_daemon_msg "Starting $DESC" "$NAME"
|
|---|
| 147 | do_start
|
|---|
| 148 | log_end_msg "$?"
|
|---|
| 149 | ;;
|
|---|
| 150 |
|
|---|
| 151 | stop)
|
|---|
| 152 | log_daemon_msg "Stopping $DESC" "$NAME"
|
|---|
| 153 | do_stop
|
|---|
| 154 | log_end_msg "$?"
|
|---|
| 155 | ;;
|
|---|
| 156 |
|
|---|
| 157 | status)
|
|---|
| 158 | status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" \
|
|---|
| 159 | && exit 0 \
|
|---|
| 160 | || exit $?
|
|---|
| 161 | ;;
|
|---|
| 162 |
|
|---|
| 163 | reload)
|
|---|
| 164 | log_daemon_msg "Reloading $DESC" "$NAME"
|
|---|
| 165 | do_reload
|
|---|
| 166 | log_end_msg "$?"
|
|---|
| 167 | ;;
|
|---|
| 168 |
|
|---|
| 169 | force-reload)
|
|---|
| 170 | log_daemon_msg "Forced reloading $DESC" "$NAME"
|
|---|
| 171 | do_force_reload
|
|---|
| 172 | log_end_msg "$RETVAL"
|
|---|
| 173 | ;;
|
|---|
| 174 |
|
|---|
| 175 | restart)
|
|---|
| 176 | log_daemon_msg "Restarting $DESC" "$NAME"
|
|---|
| 177 | do_stop
|
|---|
| 178 | case "$?" in
|
|---|
| 179 | 0)
|
|---|
| 180 | do_start
|
|---|
| 181 | log_end_msg "$?"
|
|---|
| 182 | ;;
|
|---|
| 183 | *)
|
|---|
| 184 | # Failed to stop
|
|---|
| 185 | log_end_msg 1
|
|---|
| 186 | ;;
|
|---|
| 187 | esac
|
|---|
| 188 | ;;
|
|---|
| 189 |
|
|---|
| 190 | *)
|
|---|
| 191 | echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
|
|---|
| 192 | exit 3
|
|---|
| 193 | ;;
|
|---|
| 194 | esac
|
|---|
| 195 |
|
|---|
| 196 | :
|
|---|