public
Last active

unicorn init script

  • Download Gist
unicorn
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#!/bin/bash
### BEGIN INIT INFO
# Provides: APPLICATION
# Required-Start: $all
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the APPLICATION unicorns at boot
# Description: Enable APPLICATION at boot time.
### END INIT INFO
#
# Use this as a basis for your own Unicorn init script.
# Change APPLICATION to match your app.
# Make sure that all paths are correct.
 
set -u
set -e
 
# Change these to match your app:
USER=user
APP_ROOT=path_to_current_directory
RVM_STRING=ruby-1.9.3-p0
PID="/$APP_ROOT/tmp/pids/unicorn.pid"
ENV=production
 
# assumes multi-user RVM installation
GEM_HOME="/usr/local/rvm/gems/$RVM_STRING/"
 
UNICORN_OPTS="-D -E $ENV -c $APP_ROOT/config/unicorn.rb"
 
SET_PATH="cd $APP_ROOT; rvm use $RVM_STRING; export GEM_HOME=$GEM_HOME"
CMD="$SET_PATH; bundle exec $GEM_HOME/bin/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 - $USER -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 - $USER -c "$CMD"
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - $USER -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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.