Skip to content

Instantly share code, notes, and snippets.

@dlebech
Created February 2, 2015 16:09
Show Gist options
  • Save dlebech/3fd9a86abf0a980446cf to your computer and use it in GitHub Desktop.
Save dlebech/3fd9a86abf0a980446cf to your computer and use it in GitHub Desktop.
uWSGI start/stop script that can be added to /etc/init.d on Ubuntu Linux and used with "sudo service uwsgi-emperor start"
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: uwsgi-emperor
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi emperor app server
# Description: starts uwsgi emperor app server using start-stop-daemon
### END INIT INFO
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/uwsgi
RUN_DIR=/var/run/uwsgi
LOG_DIR=/var/log/uwsgi
VASSALS_DIR=/etc/uwsgi/vassals
NAME=uwsgi
DESC=uwsgi-emperor
OWNER=www-data
GROUP=www-data
[[ -x $DAEMON ]] || exit 0
[[ -d $RUN_DIR ]] || mkdir $RUN_DIR && chown $OWNER:$GROUP $RUN_DIR
[[ -d $LOG_DIR ]] || mkdir $LOG_DIR && chown $OWNER:$GROUP $LOG_DIR
do_pid_check()
{
local PIDFILE=$1
[[ -f $PIDFILE ]] || return 0
local PID=$(cat $PIDFILE)
for p in $(pgrep $NAME); do
[[ $p == $PID ]] && return 1
done
return 0
}
do_start()
{
local PIDFILE=$RUN_DIR/$NAME.pid
local START_OPTS=" \
--emperor $VASSALS_DIR \
--pidfile $PIDFILE \
--master \
--uid www-data \
--gid www-data \
--daemonize $LOG_DIR/uwsgi-emperor.log"
if do_pid_check $PIDFILE; then
$DAEMON $START_OPTS
else
echo "Already running!"
fi
}
send_sig()
{
local PIDFILE=$RUN_DIR/$NAME.pid
set +e
[[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
set -e
}
wait_and_clean_pidfile()
{
local PIDFILE=$RUN_DIR/$NAME.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 "$1" in
start)
echo "Starting $DESC: "
do_start
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
do_stop
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC: "
do_reload
echo "$NAME."
;;
force-reload)
echo -n "Force-reloading $DESC: "
do_force_reload
echo "$NAME."
;;
restart)
echo "Restarting $DESC: "
do_stop
sleep 1
do_start
echo "$NAME."
;;
status)
get_status
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status}">&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment