Created
March 21, 2014 14:29
-
-
Save everaldo/9687463 to your computer and use it in GitHub Desktop.
Gist /etc/init.d script do Unicorn
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
################################################################################ | |
# unicorn.sh | |
################################################################################ | |
#!/usr/bin/env bash | |
set -e | |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games | |
AS_USER="USER_NAME" | |
TIMEOUT=60 | |
APP_ROOT="USER_HOME/PROJECT_DIR/current" | |
PID="USER_HOME/PROJECT_DIR/shared/tmp/pids/unicorn.pid" | |
ENVIRONMENT="production" | |
CONFIG="USER_HOME/PROJECT_DIR/shared/config/unicorn.rb" | |
UARGS="-D -c $CONFIG -E $ENVIRONMENT" | |
CMD="cd $APP_ROOT && $APP_ROOT/bin/unicorn_rails $UARGS" | |
action="$1" | |
cd $APP_ROOT || exit 1 | |
old_pid="$PID.oldbin" | |
run () { | |
if [ "$(id -un)" = "$AS_USER" ]; then | |
eval $1 | |
else | |
su -c "$1" - $AS_USER | |
fi | |
} | |
sig () { | |
test -s "$PID" && kill -$1 `cat $PID` | |
} | |
oldsig () { | |
test -s $old_pid && kill -$1 `cat $old_pid` | |
} | |
workersig () { | |
workerpid="$APP_ROOT/tmp/pids/unicorn.$2.pid" | |
test -s "$workerpid" && kill -$1 `cat $workerpid` | |
} | |
case $action in | |
start) | |
sig 0 && echo >&2 "Already running" && exit 0 | |
run "$CMD" && echo "start ok" && exit 0 | |
;; | |
stop) | |
sig QUIT && echo "stop ok" && exit 0 | |
echo >&2 "Not running" | |
;; | |
force-stop) | |
sig TERM && echo "force-stop ok" && exit 0 | |
echo >&2 "Not running" | |
;; | |
restart|reload) | |
sig HUP && echo "reloaded OK" && exit 0 | |
echo >&2 "Couldn't reload, starting '$CMD' instead" | |
run "$CMD" | |
;; | |
upgrade) | |
sig USR2 && echo Upgraded && exit 0 | |
echo >&2 "Couldn't upgrade, starting '$CMD' instead" | |
run "$CMD" | |
;; | |
# upgrade) | |
# if sig USR2 && sleep 2 && sig 0 && oldsig QUIT | |
# then | |
# n=$TIMEOUT | |
# while test -s $old_pid && test $n -ge 0 | |
# do | |
# printf '.' && sleep 1 && n=$(( $n - 1 )) | |
# done | |
# echo | |
# | |
# if test $n -lt 0 && test -s $old_pid | |
# then | |
# echo >&2 "$old_pid still exists after $TIMEOUT seconds" | |
# exit 1 | |
# fi | |
# exit 0 | |
# fi | |
# echo >&2 "Couldn't upgrade, starting '$CMD' instead" | |
# su -c "$CMD" - vagrant | |
# ;; | |
kill_worker) | |
workersig QUIT $2 && exit 0 | |
echo >&2 "Worker not running" | |
;; | |
reopen-logs) | |
sig USR1 | |
;; | |
*) | |
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment