Skip to content

Instantly share code, notes, and snippets.

@nacengineer
Created January 18, 2013 02:23
Unicorn Control Script with some Princess Bride Sprinkled in.
#!/usr/bin/env bash
# Modified for Rails 3 by Dave Southard January 17th, 2012
# Now with more princess bride!
RAILS_ENV=production
APP_ROOT=/var/rails/unicorn/$1
CURRENT_APP=$APP_ROOT/current
PID=/etc/unicorn/pids/$1.pid
OLD_PID=$PID.oldbin
START_COMMAND="bundle exec unicorn -D -E $RAILS_ENV -c /etc/unicorn/conf/$1.conf"
# Messages
MSG_INFO="Usage: $0 <start|stop|restart|force-stop>"
MSG_START_PREP="As you wish!\nAttempting to start $1."
MSG_START_SUCC="$1 has been successfully started."
MSG_START_FAIL="Couldn't start $1. Giving up and exiting."
MSG_START_EXIT="$1 is running . Exiting."
MSG_STOP_PREP="Aaasssss youuuuu wissssssh!\n Stopping $1."
MSG_STOP_SUCC="$1 successfully stopped!"
MSG_STOP_FAIL="Couldn't stop $1."
MSG_STOP_NOT_RUNNING="$1 is not running."
MSG_STOP_FORCE="Aaasssss youuuuu wissssssh!\n Forcing $1 to stop."
MSG_RESTART_PREP="Attempting restart of $1."
MSG_RESTART_SUCC="Restart was successful."
MSG_RESTART_FAIL="Couldn't restart $1."
MSG_RESTART_ELSE_01="App $1 was not running! Attempting a start."
MSG_STG_01_PREP="Stage 1: Bringing down the house."
MSG_STG_01_SUCC="Stage 1: Complete. Boy did I kill! The old code has been de-staged."
MSG_STG_01_FAIL="borked in the USR2 sig."
MSG_STG_02_PREP="Stage 2: Storm the castle."
MSG_STG_02_SUCC="Stage 2:\n My name is Inigo Montoya.\n You killed my father.\n Prepare to die!\n Old worker processes dying...\n\n"
MSG_STG_02_FAIL="Stage 2: Borked in the WINCH phase of oldsig."
MSG_STG_03_PREP="Stage 3: New Dread Pirate Robbins."
MSG_STG_03_SUCC="Stage 3: The new dread pirate is in place. Enjoy the vacation!"
MSG_STG_03_FAIL="Stage 3: BORKED in the QUIT oldsig."
MSG_NO_SUCH_APP="There is no such app."
RVM_NOT_FOUND="ERROR: An RVM installation was not found.\n"
#. /home/webteam/.bashrc
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else
printf $RVM_NOT_FOUND >&2
exit 1
fi
if [ -s $CURRENT_APP ]; then
cd $CURRENT_APP
else
echo -e $MSG_NO_SUCH_APP 1>&2
exit 1
fi
sig () {
test -s $PID && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}
case $2 in
start)
echo -e $MSG_START_PREP
(sig 0) || (echo -e >&2 $MSG_START_EXIT && exit 1)
$START_COMMAND
(sig 0 && echo -e $MESSAGE_START ) || (echo -e >&2 $MSG_START_FAIL && exit 1)
;;
stop)
echo -e $MSG_STOP_PREP
sig QUIT && exit 0
echo -e >&2 $MSG_STOP_NOT_RUNNING
;;
force-stop)
echo -e $MSG_STOP_FORCE
(sig TERM && echo -e $MSG_STOP_SUCC && exit 0) || (echo -e >&2 $MSG_STOP_FAIL && exit 1)
echo -e >&2 $MSG_STOP_FAIL
;;
restart|reload)
echo -e $MSG_RESTART_PREP
if sig 0; then
echo -e $MSG_STG_01_PREP
(sig USR2 && echo -e $MSG_STG_01_SUCC) || (echo -e >&2 $MSG_STG_01_FAIL && exit 1)
echo -e $MSG_STG_02_PREP
(oldsig WINCH && echo -e $MSG_STG_02_SUCC) || (echo -e >&2 $MSG_STG_02_FAIL && exit 1)
echo -e $MSG_STG_03_PREP
(oldsig QUIT && echo -e $MSG_STG_03_SUCC && echo -e $MSG_RESTART_SUCC) || (echo -e >&2 $MSG_STG_03_FAIL && exit 1)
else
echo -e $MSG_RESTART_ELSE_01
cd $CURRENT_APP && $START_COMMAND && echo -e $MSG_START_SUCC && exit 0
echo -e >&2 $MSG_RESTART_FAIL && exit 1
fi
exit 0
;;
*)
echo -e >&2 $MSG_INFO && exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment