run_unicorn_example.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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=6872
  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. #INIT_CONF=$APP_ROOT/config/init.conf
  17. action="$1"
  18. set -u
  19. #test -f "$INIT_CONF" && . $INIT_CONF
  20. old_pid="$PID.oldbin"
  21. cd $APP_ROOT || exit 1
  22. sig () {
  23. test -s "$PID" && kill -$1 `cat $PID`
  24. }
  25. oldsig () {
  26. test -s $old_pid && kill -$1 `cat $old_pid`
  27. }
  28. case $action in
  29. start)
  30. sig 0 && echo >&2 "Already running" && exit 0
  31. $CMD
  32. ;;
  33. stop)
  34. sig QUIT && exit 0
  35. echo >&2 "Not running"
  36. ;;
  37. force-stop)
  38. sig TERM && exit 0
  39. echo >&2 "Not running"
  40. ;;
  41. restart|reload)
  42. sig HUP && echo reloaded OK && exit 0
  43. echo >&2 "Couldn't reload, starting '$CMD' instead"
  44. $CMD
  45. ;;
  46. upgrade|r)
  47. if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  48. then
  49. n=$TIMEOUT
  50. while test -s $old_pid && test $n -ge 0
  51. do
  52. printf '.' && sleep 1 && n=$(( $n - 1 ))
  53. done
  54. echo
  55. if test $n -lt 0 && test -s $old_pid
  56. then
  57. echo >&2 "$old_pid still exists after $TIMEOUT seconds"
  58. exit 1
  59. fi
  60. exit 0
  61. fi
  62. echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  63. # $CMD
  64. ;;
  65. reopen-logs)
  66. sig USR1
  67. ;;
  68. *)
  69. echo >&2 "Usage: $0 <start|stop|restart|upgrade(r)|force-stop|reopen-logs>"
  70. exit 1
  71. ;;
  72. esac