Advertisement
Guest User

unicorn init

a guest
Jun 4th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.45 KB | None | 0 0
  1. #!/bin/sh
  2. set -e
  3.  
  4. # Feel free to change any of the following variables for your app:
  5. TIMEOUT=${TIMEOUT-60}
  6. APP_ROOT=/home/deployer/apps/xxx/current
  7. PID=$APP_ROOT/tmp/pids/unicorn.pid
  8. CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
  9. AS_USER=deployer
  10. set -u
  11.  
  12. OLD_PIN="$PID.oldbin"
  13.  
  14. sig () {
  15.   test -s "$PID" && kill -$1 `cat $PID`
  16. }
  17.  
  18. oldsig () {
  19.   test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
  20. }
  21.  
  22. run () {
  23.   if [ "$(id -un)" = "$AS_USER" ]; then
  24.     eval $1
  25.   else
  26.     su -c "$1" - $AS_USER
  27.   fi
  28. }
  29.  
  30. case "$1" in
  31. start)
  32.   sig 0 && echo >&2 "Already running" && exit 0
  33.   run "$CMD"
  34.   ;;
  35. stop)
  36.   sig QUIT && exit 0
  37.   echo >&2 "Not running"
  38.   ;;
  39. force-stop)
  40.   sig TERM && exit 0
  41.   echo >&2 "Not running"
  42.   ;;
  43. restart|reload)
  44.   sig HUP && echo reloaded OK && exit 0
  45.   echo >&2 "Couldn't reload, starting '$CMD' instead"
  46.   run "$CMD"
  47.   ;;
  48. upgrade)
  49.   if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  50.   then
  51.     n=$TIMEOUT
  52.     while test -s $OLD_PIN && test $n -ge 0
  53.     do
  54.       printf '.' && sleep 1 && n=$(( $n - 1 ))
  55.     done
  56.     echo
  57.  
  58.     if test $n -lt 0 && test -s $OLD_PIN
  59.     then
  60.       echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
  61.       exit 1
  62.     fi
  63.     exit 0
  64.   fi
  65.   echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  66.   run "$CMD"
  67.   ;;
  68. reopen-logs)
  69.   sig USR1
  70.   ;;
  71. *)
  72.   echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  73.   exit 1
  74.   ;;
  75. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement