Advertisement
madrahimov

Untitled

Apr 22nd, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.56 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # File: /etc/init.d/unicorn
  4.  
  5. ### BEGIN INIT INFO
  6. # Provides:          unicorn
  7. # Required-Start:    $local_fs $remote_fs $network $syslog
  8. # Required-Stop:     $local_fs $remote_fs $network $syslog
  9. # Default-Start:     2 3 4 5
  10. # Default-Stop:      0 1 6
  11. # Short-Description: starts the unicorn web server
  12. # Description:       starts unicorn
  13. ### END INIT INFO
  14.  
  15. # Feel free to change any of the following variables for your app:
  16.  
  17. # ubuntu is the default user on Amazon's EC2 Ubuntu instances.
  18. USER=deploy
  19. # Replace [PATH_TO_RAILS_ROOT_FOLDER] with your application's path. I prefer
  20. # /srv/app-name to /var/www. The /srv folder is specified as the server's
  21. # "service data" folder, where services are located. The /var directory,
  22. # however, is dedicated to variable data that changes rapidly, such as logs.
  23. # Reference https://help.ubuntu.com/community/LinuxFilesystemTreeOverview for
  24. # more information.
  25. APP_ROOT=/data/bantikov/current
  26.  
  27. # Set the environment. This can be changed to staging or development for staging
  28. # servers.
  29. RAILS_ENV=production
  30.  
  31. # This should match the pid setting in $APP_ROOT/config/unicorn.rb.
  32. PID=/data/bantikov/shared/pids/unicorn.pid
  33.  
  34. # A simple description for service output.
  35. DESC="Bantikov - $RAILS_ENV"
  36.  
  37. # If you're using rbenv, you may need to use the following setup to get things
  38. # working properly:
  39. # RBENV_RUBY_VERSION=`cat $APP_ROOT/.ruby-version`
  40. # RBENV_ROOT="/home/$USER/.rbenv"
  41. # PATH="$RBENV_ROOT/bin:$PATH"
  42. # SET_PATH="cd $APP_ROOT && rbenv rehash && rbenv local $RBENV_RUBY_VERSION"
  43. SET_PATH="cd $APP_ROOT"
  44.  
  45.  
  46. # Unicorn can be run using `bundle exec unicorn` or `bin/unicorn`.
  47. UNICORN="bin/unicorn"
  48.  
  49. # Execute the unicorn executable as a daemon, with the appropriate configuration
  50. # and in the appropriate environment.
  51. UNICORN_OPTS="-c $APP_ROOT/config/unicorn.rb -E $RAILS_ENV -D"
  52. CMD="$SET_PATH && $UNICORN $UNICORN_OPTS"
  53.  
  54. CMD_INDEX="cd /data/bantikov/current && bundle exec rake RAILS_ENV=production ts:rebuild"
  55. CMD_DELAYED="cd /data/bantikov/current;RAILS_ENV=production script/delayed_job restart"
  56.  
  57. # Give your upgrade action a timeout of 60 seconds.
  58. TIMEOUT=60
  59.  
  60. # Store the action that we should take from the service command's first
  61. # argument (e.g. start, stop, upgrade).
  62. action="$1"
  63.  
  64. # Make sure the script exits if any variables are unset. This is short for
  65. # set -o nounset.
  66. set -u
  67.  
  68. # Set the location of the old pid. The old pid is the process that is getting
  69. # replaced.
  70. old_pid="$PID.oldbin"
  71.  
  72. # Make sure the APP_ROOT is actually a folder that exists. An error message from
  73. # the cd command will be displayed if it fails.
  74. cd $APP_ROOT || exit 1
  75.  
  76. # A function to send a signal to the current unicorn master process.
  77. sig () {
  78.   test -s "$PID" && kill -$1 `cat $PID`
  79. }
  80.  
  81. # Send a signal to the old process.
  82. oldsig () {
  83.   test -s $old_pid && kill -$1 `cat $old_pid`
  84. }
  85.  
  86. # A switch for handling the possible actions to take on the unicorn process.
  87. case $action in
  88.   # Start the process by testing if it's there (sig 0), failing if it is,
  89.   # otherwise running the command as specified above.
  90.   start)
  91.     sig 0 && echo >&2 "$DESC is already running" && exit 0
  92.     su - $USER -c "$CMD"
  93.     su - $USER -c "$CMD_INDEX"
  94.     su - $USER -c "$CMD_DELAYED"
  95.     ;;
  96.  
  97.   # Graceful shutdown. Send QUIT signal to the process. Requests will be
  98.   # completed before the processes are terminated.
  99.   stop)
  100.     sig QUIT && echo "Stopping $DESC" exit 0
  101.     echo >&2 "Not running"
  102.     ;;
  103.  
  104.   # Quick shutdown - kills all workers immediately.
  105.   force-stop)
  106.     sig TERM && echo "Force-stopping $DESC" && exit 0
  107.     echo >&2 "Not running"
  108.     ;;
  109.  
  110.   # Graceful shutdown and then start.
  111.   restart)
  112.     sig QUIT && echo "Restarting $DESC" && sleep 2 \
  113.       && su - $USER -c "$CMD" && exit 0
  114.     echo >&2 "Couldn't restart."
  115.     ;;
  116.  
  117.   # Reloads config file (unicorn.rb) and gracefully restarts all workers. This
  118.   # command won't pick up application code changes if you have `preload_app
  119.   # true` in your unicorn.rb config file.
  120.   reload)
  121.     sig HUP && echo "Reloading configuration for $DESC" && exit 0
  122.     echo >&2 "Couldn't reload configuration."
  123.     ;;
  124.  
  125.   # Re-execute the running binary, then gracefully shutdown old process. This
  126.   # command allows you to have zero-downtime deployments. The application may
  127.   # spin for a minute, but at least the user doesn't get a 500 error page or
  128.   # the like. Unicorn interprets the USR2 signal as a request to start a new
  129.   # master process and phase out the old worker processes. If the upgrade fails
  130.   # for some reason, a new process is started.
  131.   upgrade)
  132.     if sig USR2 && echo "Upgrading $DESC" && sleep 10 \
  133.       && sig 0 && oldsig QUIT
  134.     then
  135.       n=$TIMEOUT
  136.       while test -s $old_pid && test $n -ge 0
  137.       do
  138.         printf '.' && sleep 1 && n=$(( $n - 1 ))
  139.       done
  140.       echo
  141.  
  142.       if test $n -lt 0 && test -s $old_pid
  143.       then
  144.         echo >&2 "$old_pid still exists after $TIMEOUT seconds"
  145.         exit 1
  146.       fi
  147.       exit 0
  148.     fi
  149.     echo >&2 "Couldn't upgrade, starting 'su - $USER -c \"$CMD\"' instead"
  150.     su - $USER -c "$CMD"
  151.     ;;
  152.  
  153.   # A basic status checker. Just checks if the master process is responding to
  154.   # the `kill` command.
  155.   status)
  156.     sig 0 && echo >&2 "$DESC is running." && exit 0
  157.     echo >&2 "$DESC is not running."
  158.     ;;
  159.  
  160.   # Reopen all logs owned by the master and all workers.
  161.   reopen-logs)
  162.     sig USR1
  163.     ;;
  164.  
  165.   # Any other action gets the usage message.
  166.   *)
  167.     # Usage
  168.     echo >&2 "Usage: $0 <start|stop|restart|reload|upgrade|force-stop|reopen-logs>"
  169.     exit 1
  170.     ;;
  171. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement