<% @path = "/etc/init.d/unicorn" @perms = 0755 current_path = "/mnt/#{rubber_env.app_name}-#{Rubber.env}/current" %> #!/bin/sh set -e # Example init script, this can be used with nginx, too, # since nginx and unicorn accept the same signals # Feel free to change any of the following variables for your app: TIMEOUT=${TIMEOUT-600} APP_ROOT=<%= current_path %> PID=<%= rubber_env.unicorn_pid_file %> CMD="<%= "bundle exec unicorn_rails -c #{current_path}/config/unicorn.rb -E #{Rubber.env} -D" %>" action="$1" set -u 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 $action in start) sig 0 && echo >&2 "Already running" && exit 0 $CMD ;; stop) sig QUIT && exit 0 echo >&2 "Not running" ;; kill) 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) if sig USR2 then printf 'Waiting for new Unicorn workers' n=$TIMEOUT while (! (test -s $PID && ps --no-headers --ppid `cat $PID` > /dev/null)) && test $n -ge 0 do printf '.' && sleep 10 && n=$(( $n - 10 )) done if test $n -lt 0 && (! (test -s $PID && ps --no-headers --ppid `cat $PID` > /dev/null)) then echo >&2 "$PID process does not exist after $TIMEOUT seconds" exit 1 fi printf '\nNew Unicorn workers started' if test -s $old_pid then printf '\nWaiting for old Unicorn master to stop' n=$TIMEOUT while test -s $old_pid && test $n -ge 0 do printf '.' && sleep 10 && n=$(( $n - 10 )) done echo if test $n -lt 0 && test -s $old_pid then echo >&2 "$old_pid still exists after $TIMEOUT seconds" exit 1 fi fi printf '\nOld Unicorn master stopped' exit 0 fi echo >&2 "Couldn't upgrade, starting '$CMD' instead" $CMD ;; status) sig 0 && echo >&2 "Running" && exit 1 echo >&2 "Stopped" && exit 0 ;; reopen-logs) sig USR1 ;; *) echo >&2 "Usage: $0 " exit 1 ;; esac