-
-
Save kradalby/3252b8bacca6622bf864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
### BEGIN INIT INFO | |
# Provides: 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=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
DAEMON=/usr/local/bin/uwsgi | |
RUN=/var/run/uwsgi | |
ENABLED_CONFIGS_DIR=/etc/uwsgi/apps-enabled | |
AVAILABLE_CONFIGS_DIR=/etc/uwsgi/apps-available | |
NAME=uwsgi | |
DESC=emperor | |
OWNER=nginx | |
GROUP=nginx | |
OP=$1 | |
[[ -x $DAEMON ]] || exit 0 | |
[[ -d $RUN ]] || mkdir $RUN && chown $OWNER.$GROUP $RUN | |
DAEMON_OPTS="" | |
# Include uwsgi defaults if available | |
if [[ -f /etc/default/uwsgi ]]; then | |
. /etc/default/uwsgi | |
fi | |
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/$NAME.pid | |
local START_OPTS=" \ | |
--emperor $ENABLED_CONFIGS_DIR \ | |
--pidfile $PIDFILE \ | |
--daemonize /var/log/$NAME/uwsgi-emperor.log \ | |
--uid $OWNER \ | |
--gid $GROUP" | |
if do_pid_check $PIDFILE; then | |
$NAME $DAEMON_OPTS $START_OPTS | |
else | |
echo "Already running!" | |
fi | |
} | |
send_sig() | |
{ | |
local PIDFILE=$RUN/$NAME.pid | |
set +e | |
[[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1 | |
set -e | |
} | |
wait_and_clean_pidfile() | |
{ | |
local PIDFILE=$RUN/uwsgi.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 | |
} | |
enable_configs() | |
{ | |
local configs | |
if [[ $# -eq 0 || ${1,,} = 'all' ]]; then | |
configs=$(diff $AVAILABLE_CONFIGS_DIR $ENABLED_CONFIGS_DIR \ | |
| grep $AVAILABLE_CONFIGS_DIR \ | |
| sed -re 's#.+: (.+)$#\1#') | |
else | |
configs=$@ | |
fi | |
for c in $configs; do | |
echo -n "Enabling $c..." | |
[[ -f $ENABLED_CONFIGS_DIR/$c ]] && echo "Skipped" && continue | |
[[ -f $AVAILABLE_CONFIGS_DIR/$c ]] && \ | |
ln -s $AVAILABLE_CONFIGS_DIR/$c $ENABLED_CONFIGS_DIR && \ | |
echo "Done" && \ | |
continue | |
echo "Error" | |
done | |
} | |
disable_configs() | |
{ | |
local configs | |
if [[ $# -eq 0 || ${1,,} = 'all' ]]; then | |
configs=$(find $ENABLED_CONFIGS_DIR -type l -exec basename {} \;) | |
else | |
configs=$@ | |
fi | |
for c in $configs; do | |
local config_path="$ENABLED_CONFIGS_DIR/$c" | |
echo -n "Disabling $c..." | |
[[ ! -L $config_path ]] && echo "Skipped" && continue | |
[[ -f $config_path ]] && rm $config_path && echo "Done" && continue | |
echo "Error" | |
done | |
} | |
case "$OP" 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 | |
;; | |
enable) | |
shift | |
enable_configs $@ | |
;; | |
disable) | |
shift | |
disable_configs $@ | |
;; | |
*) | |
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