|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +### BEGIN INIT INFO |
| 4 | +# Provides: gunicorn_django |
| 5 | +# Required-Start: $all |
| 6 | +# Required-Stop: $all |
| 7 | +# Default-Start: 2 3 4 5 |
| 8 | +# Default-Stop: 0 1 6 |
| 9 | +# Short-Description: starts the pyscada gunicorn server |
| 10 | +# Description: starts pyscada gunicorn using start-stop-daemon |
| 11 | +### END INIT INFO |
| 12 | + |
| 13 | +# sample configfile for /etc/default/gunicorn_django |
| 14 | +# SERVERS=( |
| 15 | +# 'APP_NAME DJANGODIR number_of_workers' |
| 16 | +# ) |
| 17 | +# RUN_AS='www-data' |
| 18 | + |
| 19 | +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
| 20 | +if [ -f /etc/default/gunicorn_django ] ; then |
| 21 | + . /etc/default/gunicorn_django |
| 22 | +fi |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +start () { |
| 27 | + |
| 28 | + for i in "${SERVERS[@]}" |
| 29 | + do |
| 30 | + : |
| 31 | + set -- "$i" |
| 32 | + IFS=" "; declare -a data=($*) |
| 33 | + |
| 34 | + if [ "$SERVER" ]; then |
| 35 | + if [ "$SERVER" != ${data[0]} ]; then |
| 36 | + continue |
| 37 | + fi |
| 38 | + fi |
| 39 | + if [ -e ${data[1]}/run/gunicorn.pid ] |
| 40 | + then |
| 41 | + PID=$(cat ${data[1]}/run/gunicorn.pid) |
| 42 | + if ps -p $PID > /dev/null |
| 43 | + then |
| 44 | + echo "gunicorn service is already running ($PID)" |
| 45 | + exit 0 |
| 46 | + fi |
| 47 | + fi |
| 48 | + |
| 49 | + echo "Spawning ${data[0]}" |
| 50 | + # Create the run directory if it doesn't exist |
| 51 | + RUNDIR=$(dirname ${data[1]}/run/gunicorn.sock) |
| 52 | + test -d $RUNDIR || mkdir -p $RUNDIR |
| 53 | + export DJANGO_SETTINGS_MODULE=${data[0]}.settings |
| 54 | + export PYTHONPATH=${data[1]}:$PYTHONPATH |
| 55 | + # Start your Django Unicorn |
| 56 | + # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon) |
| 57 | + start-stop-daemon --start --quiet -c $RUN_AS -d ${data[1]} --pidfile ${data[1]}/run/gunicorn.pid --exec /usr/local/bin/gunicorn -- ${data[0]}.wsgi:application -n ${data[0]} -w ${data[2]} -u $RUN_AS -b unix:${data[1]}/run/gunicorn.sock -p ${data[1]}/run/gunicorn.pid -D |
| 58 | + |
| 59 | + done |
| 60 | + return |
| 61 | +} |
| 62 | + |
| 63 | +stop () { |
| 64 | + for i in "${SERVERS[@]}" |
| 65 | + do |
| 66 | + : |
| 67 | + set -- "$i" |
| 68 | + IFS=" "; declare -a data=($*) |
| 69 | + if [ "$SERVER" ]; then |
| 70 | + if [ "$SERVER" != ${data[0]} ]; then |
| 71 | + continue |
| 72 | + fi |
| 73 | + fi |
| 74 | + if [ -e ${data[1]}/run/gunicorn.pid ] |
| 75 | + then |
| 76 | + kill -TERM $(cat ${data[1]}/run/gunicorn.pid) |
| 77 | + echo "stopped service" |
| 78 | + else |
| 79 | + echo "service not running" |
| 80 | + fi |
| 81 | + done |
| 82 | +} |
| 83 | + |
| 84 | +case "$1" in |
| 85 | + start) |
| 86 | + echo "Starting" |
| 87 | + start |
| 88 | + ;; |
| 89 | + stop) |
| 90 | + echo "Stopping" |
| 91 | + stop |
| 92 | + ;; |
| 93 | + restart) |
| 94 | + echo "Restarting" |
| 95 | + stop |
| 96 | + sleep 1 |
| 97 | + start |
| 98 | + ;; |
| 99 | + *) |
| 100 | + N=/etc/init.d/$NAME |
| 101 | + echo "Usage: $N {start|stop|restart} " >&2 |
| 102 | + exit 1 |
| 103 | + ;; |
| 104 | +esac |
| 105 | + |
| 106 | +exit 0 |
0 commit comments