Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Last active December 17, 2015 15:59
Show Gist options
  • Save radiosilence/5635766 to your computer and use it in GitHub Desktop.
Save radiosilence/5635766 to your computer and use it in GitHub Desktop.
A Debian init script for uWSGI
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi app server
# Description: starts uwsgi app server using start-stop-daemon
### END INIT INFO
set -e
. /lib/lsb/init-functions
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/uwsgi/bin/uwsgi
RUN=/var/run/uwsgi
ENABLED_CONFIGS_DIR=/srv/*/*/uwsgi.*
USER=deploy
GROUP=www-data
NAME=uwsgi
DESC=uWSGI
VERSION=$NAME
OP=$1
[[ -x $DAEMON ]] || exit 0
[[ -d $RUN ]] || mkdir $RUN && chown $USER $RUN
DAEMON_OPTS=""
# Include uwsgi defaults if available
if [[ -f /etc/default/$VERSION ]]; then
. /etc/default/$VERSION
fi
do_pid_check()
{
local PIDFILE=$1
[[ -f $PIDFILE ]] || return 0
local PID=$(cat $PIDFILE)
for p in $(pgrep $VERSION); do
[[ $p == $PID ]] && return 1
done
return 0
}
do_start()
{
local PIDFILE=$RUN/$VERSION.pid
local START_OPTS=" \
--pidfile $PIDFILE \
--daemonize /var/log/$NAME \
--emperor-tyrant \
--enable-threads \
"
if do_pid_check $PIDFILE; then
$DAEMON $DAEMON_OPTS $START_OPTS --emperor "$ENABLED_CONFIGS_DIR" --emperor "/opt/uwsgi/etc/uwsgi.d/*.ini"
else
echo "Already running!"
fi
}
send_sig()
{
local PIDFILE=$RUN/$VERSION.pid
set +e
[[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
set -e
}
wait_and_clean_pidfile()
{
local PIDFILE=$RUN/$VERSION.pid
until do_pid_check $PIDFILE; do
echo -n "";
done
rm -f $PIDFILE
}
do_stop()
{
send_sig -3
wait_and_clean_pidfile
}
do_reload()
{
send_sig -1
}
do_force_reload()
{
send_sig -15
}
get_status()
{
send_sig -10
}
case "$OP" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
do_start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
log_end_msg $?
;;
reload)
log_daemon_msg "Reloading $DESC" "$NAME"
do_reload
log_end_msg $?
;;
force-reload)
log_daemon_msg "Force-reloading $DESC" "$NAME"
do_reload
do_force_reload
log_end_msg $?
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
sleep 1
do_start
log_end_msg $?
;;
status)
get_status
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status"
"|enable|disable}">&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment