Created
September 6, 2011 03:11
-
-
Save yetanothernguyen/1196492 to your computer and use it in GitHub Desktop.
unicorn init script
This file contains hidden or 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
#!/bin/bash | |
### BEGIN INIT INFO | |
# Provides: says-dot-com-production | |
# Required-Start: $all | |
# Required-Stop: $network $local_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start the says-dot-com-production unicorns at boot | |
# Description: Enable says-dot-com-staging at boot time. | |
### END INIT INFO | |
# | |
# Use this as a basis for your own Unicorn init script. | |
# Change says-dot-com-staging to match your app. | |
# Make sure that all paths are correct. | |
set -u | |
set -e | |
TIMEOUT=${TIMEOUT-60} | |
APP_NAME=says-dot-com-production | |
APP_ROOT="/var/www/$APP_NAME/current" | |
APP_SHARED_ROOT="/var/www/$APP_NAME/shared" | |
PID="/var/www/$APP_NAME/shared/pids/unicorn.pid" | |
ENV=production | |
UNICORN_OPTS="-D -E $ENV -c $APP_SHARED_ROOT/config/unicorn.rb" | |
CMD="cd $APP_ROOT; bundle exec unicorn $UNICORN_OPTS" | |
old_pid="$PID.oldbin" | |
cd $APP_ROOT || exit 1 | |
sig () { | |
test -s "$PID" && kill -$1 `cat $PID` | |
} | |
oldsig () { | |
test -s $old_pid && kill -$1 `cat $old_pid` | |
} | |
case ${1-help} in | |
start) | |
sig 0 && echo >&2 "Already running" && exit 0 | |
su - ubuntu -c "$CMD" | |
;; | |
stop) | |
sig QUIT && exit 0 | |
echo >&2 "Not running" | |
;; | |
force-stop) | |
sig TERM && exit 0 | |
echo >&2 "Not running" | |
;; | |
restart|reload) | |
sig HUP && echo reloaded OK && exit 0 | |
echo >&2 "Couldn't reload, starting '$CMD' instead" | |
su - ubuntu -c "$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 - ubuntu -c "$CMD" | |
;; | |
rotate) | |
sig USR1 && echo rotated logs OK && exit 0 | |
echo >&2 "Couldn't rotate logs" && exit 1 | |
;; | |
*) | |
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked well for me. The only thing I noticed is that unicorn ignores PID, so it had to be added to the config file.