Skip to content

Instantly share code, notes, and snippets.

@ethier
Last active December 26, 2015 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ethier/7190310 to your computer and use it in GitHub Desktop.
Save ethier/7190310 to your computer and use it in GitHub Desktop.
Unicorn shell script
#!/usr/bin/env bash
set -e
### Unicorn variables ###
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/path/to/project
PID_PATH=$APP_ROOT/tmp/pids
PID=$PID_PATH/unicorn.pid
USER=ubuntu
GROUP=ubuntu
# See http://technology.customink.com/blog/2012/03/16/simple-garbage-collection-tuning-for-rails/
export RUBY_GC_MALLOC_LIMIT=60000000
export RUBY_HEAP_MIN_SLOTS=1800000 # Slots Live + 20%
export RUBY_FREE_MIN=18000 # 1% of HEAP_MIN_SLOTS
export RUBY_HEAP_SLOTS_INCREMENT=144000 # 8% of HEAP_MIN_SLOTS
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export LD_PRELOAD=/usr/lib/libtcmalloc_minimal.so.0.1.0
CMD="$APP_ROOT/bin/unicorn -D -E $RAILS_ENV -c $APP_ROOT/config/unicorn.rb"
INIT_CONF=$APP_ROOT/config/unicorn.conf
action="$1"
set -u
test -f "$INIT_CONF" && . $INIT_CONF
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`
}
workersig () {
workerpid=$PID_PATH/worker.$2.pid
test -s "$workerpid" && kill -$1 `cat $workerpid`
}
create_pid_path () {
test -d $PID_PATH || ( mkdir -p $PID_PATH && chown $USER.$GROUP $PID_PATH )
}
run () {
if [ "$(id -un)" = "$USER" ]; then
eval $1
else
su -c "$1" - $USER
fi
}
case $action in
start)
create_pid_path
sig 0 && echo >&2 "Already running" && exit 0
run "$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"
run "$CMD"
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
status)
sig 0 && echo "running with pid `cat $PID`" && exit 0
echo stopped && exit 1
;;
kill_worker)
workersig QUIT $2 && exit 0
echo >&2 "Worker not running"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|status|restart|upgrade|status|force-stop|reopen-logs|kill_worker>"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment