Skip to content

Instantly share code, notes, and snippets.

@suwa320
Created August 20, 2012 08:58
Show Gist options
  • Select an option

  • Save suwa320/3402478 to your computer and use it in GitHub Desktop.

Select an option

Save suwa320/3402478 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# referred to
# http://www.stopdropandrew.com/2010/06/01/where-unicorns-go-to-die-watching-unicorn-workers-with-monit.html
#
if test -z "$RACK_ENV"; then
RACK_ENV=production
fi
if test -f ~/.rvm/scripts/rvm -o "$(type -t rvm)" = "function"; then
source ~/.rvm/scripts/rvm
fi
if test -z "$RVM_RUBY_VERSION"; then
RVM_RUBY_VERSION=default
fi
rvm use $RVM_RUBY_VERSION
set -e
set -u
APP_ROOT=$1
if test -f "$APP_ROOT/config/unicorn.conf"; then
UNICORN_CONF=$APP_ROOT/config/unicorn.conf
else
UNICORN_CONF=$APP_ROOT/unicorn.conf
fi
PID=$APP_ROOT/tmp/unicorn.pid
CMD="bundle exec unicorn -D -E $RACK_ENV -c $UNICORN_CONF config.ru"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
workersig () {
workerpid="$APP_ROOT/tmp/unicorn_worker.$2.pid"
test -s "$workerpid" && kill -$1 `cat $workerpid`
}
cd $APP_ROOT || exit 1
case $2 in
start)
sig 0 && echo >&2 "Already running" && exit 0
$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"
$CMD
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
$CMD
;;
kill_worker)
workersig QUIT $3 && exit 0
echo >&2 "Worker not running"
;;
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