run_unicorn_docker.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/sh
  2. set -e
  3. # Example init script, this can be used with nginx, too,
  4. # since nginx and unicorn accept the same signals
  5. # Feel free to change any of the following variables for your app:
  6. TIMEOUT=${TIMEOUT-60}
  7. APP_ROOT=`pwd`
  8. PID=$APP_ROOT/tmp/pids/unicorn.pid
  9. PORT=6876
  10. #MODE=production
  11. MODE=development
  12. #MODE="test"
  13. #CMD="/usr/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
  14. # bundle exec rake assets:precompile
  15. #CMD="unicorn_rails -c $APP_ROOT/config/unicorn.rb -E $MODE -p $PORT -D"
  16. CMD="unicorn_rails -c $APP_ROOT/config/unicorn.rb -E $MODE -p $PORT"
  17. #INIT_CONF=$APP_ROOT/config/init.conf
  18. action="$1"
  19. set -u
  20. #test -f "$INIT_CONF" && . $INIT_CONF
  21. old_pid="$PID.oldbin"
  22. cd $APP_ROOT || exit 1
  23. sig () {
  24. test -s "$PID" && kill -$1 `cat $PID`
  25. }
  26. oldsig () {
  27. test -s $old_pid && kill -$1 `cat $old_pid`
  28. }
  29. case $action in
  30. start)
  31. sig 0 && echo >&2 "Already running" && exit 0
  32. $CMD
  33. ;;
  34. stop)
  35. sig QUIT && exit 0
  36. echo >&2 "Not running"
  37. ;;
  38. force-stop)
  39. sig TERM && exit 0
  40. echo >&2 "Not running"
  41. ;;
  42. restart|reload)
  43. sig HUP && echo reloaded OK && exit 0
  44. echo >&2 "Couldn't reload, starting '$CMD' instead"
  45. $CMD
  46. ;;
  47. upgrade|r)
  48. if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  49. then
  50. n=$TIMEOUT
  51. while test -s $old_pid && test $n -ge 0
  52. do
  53. printf '.' && sleep 1 && n=$(( $n - 1 ))
  54. done
  55. echo
  56. if test $n -lt 0 && test -s $old_pid
  57. then
  58. echo >&2 "$old_pid still exists after $TIMEOUT seconds"
  59. exit 1
  60. fi
  61. exit 0
  62. fi
  63. echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  64. # $CMD
  65. ;;
  66. reopen-logs)
  67. sig USR1
  68. ;;
  69. *)
  70. echo >&2 "Usage: $0 <start|stop|restart|upgrade(r)|force-stop|reopen-logs>"
  71. exit 1
  72. ;;
  73. esac