Created
January 21, 2014 20:54
-
-
Save psi/8548183 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| exec 2>&1 | |
| function is_unicorn_alive { | |
| set +e | |
| if [ -n $1 ] && kill -0 $1 >/dev/null 2>&1; then | |
| echo "yes" | |
| fi | |
| set -e | |
| } | |
| CUR_PID_FILE=/home/beyond/app/shared/pids/unicorn.pid | |
| OLD_PID_FILE=$CUR_PID_FILE.oldbin | |
| if [ -e $OLD_PID_FILE ]; then | |
| OLD_PID=$(cat $OLD_PID_FILE) | |
| echo "Waiting for existing master ($OLD_PID) to exit" | |
| while [ -n "$(is_unicorn_alive $OLD_PID)" ]; do | |
| /bin/echo -n '.' | |
| sleep 2 | |
| done | |
| fi | |
| if [ -e $CUR_PID_FILE ]; then | |
| CUR_PID=$(cat $CUR_PID_FILE) | |
| if [ -n "$(is_unicorn_alive $CUR_PID)" ]; then | |
| echo "Unicorn master already running. PID: $CUR_PID" | |
| RUNNING=true | |
| fi | |
| fi | |
| if [ ! $RUNNING ]; then | |
| echo "Starting unicorn" | |
| cd /home/beyond/app/current | |
| exec /usr/bin/chpst \ | |
| -u beyond:beyond \ | |
| bundle exec \ | |
| unicorn \ | |
| -E production \ | |
| -c /etc/unicorn/beyond.rb -D | |
| sleep 3 | |
| CUR_PID=$(cat $CUR_PID_FILE) | |
| fi | |
| function restart { | |
| echo "Initialize new master with USR2" | |
| kill -USR2 $CUR_PID | |
| # Make runit restart to pick up new unicorn pid | |
| sleep 2 | |
| echo "Restarting service to capture new pid" | |
| exit | |
| } | |
| function graceful_shutdown { | |
| echo "Initializing graceful shutdown" | |
| kill -QUIT $CUR_PID | |
| } | |
| function unicorn_interrupted { | |
| echo "Unicorn process interrupted. Possibly a runit thing?" | |
| } | |
| trap restart HUP QUIT USR2 INT | |
| trap graceful_shutdown TERM KILL | |
| trap unicorn_interrupted ALRM | |
| echo "Waiting for current master to die. PID: ($CUR_PID)" | |
| while [ -n "$(is_unicorn_alive $CUR_PID)" ]; do | |
| /bin/echo -n '.' | |
| sleep 2 | |
| done | |
| echo "You've killed a unicorn!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment