Skip to content

Instantly share code, notes, and snippets.

@troex
Created April 4, 2011 20:57

Revisions

  1. troex revised this gist Aug 9, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    #
    ## RAILS_ENV=production
    ## RAILS_ROOT=/var/apps/www/my_app/current
    ## PID=$RAILS_ROOT/tmp/unicorn.pid
    ## PID=$RAILS_ROOT/tmp/pids/unicorn.pid
    ## START_CMD="bundle exec unicorn_rails"
    ## USER="www-data"
    ## RESTART_SLEEP=5
    @@ -27,8 +27,8 @@
    ## APP_ROOT = ENV["RAILS_ROOT"]
    ## RAILS_ENV = ENV["RAILS_ENV"]
    ##
    ## pid "#{APP_ROOT}/tmp/unicorn.pid"
    ## listen "#{APP_ROOT}/tmp/unicorn.sock"
    ## pid "#{APP_ROOT}/tmp/pids/unicorn.pid"
    ## listen "#{APP_ROOT}/tmp/sockets/unicorn.sock"
    ## stderr_path "#{APP_ROOT}/log/unicorn_error.log"
    ##
    ## working_directory "#{APP_ROOT}"
    @@ -125,7 +125,7 @@ handle_delayed_job () {

    case $1 in
    start|stop|restart|reload|status)
    CMD="RAILS_ENV=$RAILS_ENV bundle exec ./script/delayed_job $1"
    CMD="env RAILS_ENV=$RAILS_ENV bundle exec ./script/delayed_job $1"
    if [ "$USER" != `whoami` ]; then
    CMD="sudo -u $USER -- env $CMD"
    fi
  2. troex revised this gist Aug 9, 2012. 1 changed file with 67 additions and 13 deletions.
    80 changes: 67 additions & 13 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -9,15 +9,30 @@
    # Description: Unicorn is an HTTP server for Rack application
    ### END INIT INFO

    # Author: Troex Nevelin <troex@fury.scancode.ru>
    # based on http://gist.github.com/308216 by http://github.com/mguterl

    # A sample /etc/unicorn/my_app.conf, only RAILS_ROOT is required, other are optional
    #
    ## A sample /etc/unicorn/my_app.conf
    ##
    ## RAILS_ENV=production
    ## RAILS_ROOT=/var/apps/www/my_app/current
    ## PID=$RAILS_ROOT/tmp/unicorn.pid
    ## START_CMD="bundle exec unicorn"
    ## START_CMD="bundle exec unicorn_rails"
    ## USER="www-data"
    ## RESTART_SLEEP=5
    ## HANDLE_DELAYED_JOB=true

    # A recommended /etc/unicorn/my_app.unicorn.rb
    #
    ## APP_ROOT = ENV["RAILS_ROOT"]
    ## RAILS_ENV = ENV["RAILS_ENV"]
    ##
    ## pid "#{APP_ROOT}/tmp/unicorn.pid"
    ## listen "#{APP_ROOT}/tmp/unicorn.sock"
    ## stderr_path "#{APP_ROOT}/log/unicorn_error.log"
    ##
    ## working_directory "#{APP_ROOT}"
    ## worker_processes 1

    set -e

    @@ -30,12 +45,11 @@ oldsig () {
    }

    cmd () {

    case $1 in
    start)
    sig 0 && echo >&2 "Already running" && return
    echo "Starting"
    `$CMD`
    $CMD
    ;;
    stop)
    sig QUIT && echo "Stopping" && return
    @@ -46,7 +60,7 @@ cmd () {
    echo >&2 "Not running"
    ;;
    restart|reload)
    sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && return
    sig USR2 && sleep $RESTART_SLEEP && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && return
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    $CMD
    ;;
    @@ -64,7 +78,7 @@ cmd () {
    echo >&2 "Not running" && return
    ;;
    *)
    echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
    echo >&2 "Usage: $0 <start|stop|restart|reload|status|upgrade|rotate|force-stop>"
    return
    ;;
    esac
    @@ -74,13 +88,22 @@ setup () {
    echo -n "$RAILS_ROOT: "
    cd $RAILS_ROOT || exit 1

    if [ -z "$RAILS_ENV" ]; then
    RAILS_ENV=development
    fi

    if [ -z "$PID" ]; then
    PID=$RAILS_ROOT/tmp/pids/unicorn.pid
    fi

    if [ -z "$RESTART_SLEEP" ]; then
    RESTART_SLEEP=5
    fi

    export PID
    export OLD_PID="$PID.oldbin"
    export RAILS_ROOT
    export RESTART_SLEEP

    if [ -z "$START_CMD" ]; then
    START_CMD="bundle exec unicorn_rails"
    @@ -94,18 +117,39 @@ setup () {
    #echo $CMD
    }

    handle_delayed_job () {
    # $1 contains command
    if [ "$HANDLE_DELAYED_JOB" != "true" ]; then
    return
    fi

    case $1 in
    start|stop|restart|reload|status)
    CMD="RAILS_ENV=$RAILS_ENV bundle exec ./script/delayed_job $1"
    if [ "$USER" != `whoami` ]; then
    CMD="sudo -u $USER -- env $CMD"
    fi
    $CMD
    ;;
    esac
    }

    start_stop () {

    # either run the start/stop/reload/etc command for every config under /etc/unicorn
    # or just do it for a specific one

    # $1 contains the start/stop/etc command
    # $2 if it exists, should be the specific config we want to act on
    if [ -f "/etc/unicorn/$2.conf" ]; then
    . /etc/unicorn/$2.conf
    export UNICORN_CONFIG="/etc/unicorn/$2.unicorn.rb"
    setup
    cmd $1
    if [ -n "$2" ]; then
    if [ -f "/etc/unicorn/$2.conf" ]; then
    . /etc/unicorn/$2.conf
    export UNICORN_CONFIG="/etc/unicorn/$2.unicorn.rb"
    setup
    cmd $1
    handle_delayed_job $1
    else
    echo >&2 "/etc/unicorn/$2.conf: not found"
    fi
    else
    for CONFIG in /etc/unicorn/*.conf; do
    # import the variables
    @@ -115,7 +159,17 @@ start_stop () {

    # run the start/stop/etc command
    cmd $1
    handle_delayed_job $1

    # clean enviroment
    unset PID
    unset OLD_PID
    unset RAILS_ROOT
    unset RAILS_ENV
    unset CMD
    unset USER
    unset START_CMD
    unset UNICORN_CONFIG
    done
    fi
    }
  3. troex revised this gist Feb 16, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -88,7 +88,7 @@ setup () {
    CMD="$START_CMD -c $UNICORN_CONFIG -E $RAILS_ENV -D"

    if [ "$USER" != `whoami` ]; then
    CMD="sudo -u $USER -- $CMD"
    CMD="sudo -u $USER -- env RAILS_ROOT=$RAILS_ROOT $CMD"
    fi
    export CMD
    #echo $CMD
  4. troex revised this gist Dec 1, 2011. 1 changed file with 0 additions and 0 deletions.
    Empty file modified unicorn.sh
    100644 → 100755
    Empty file.
  5. troex revised this gist Dec 1, 2011. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: thin
    # Provides: unicorn
    # Required-Start: $local_fs $remote_fs mysql
    # Required-Stop: $local_fs $remote_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: thin initscript
    # Description: thin
    # Short-Description: unicorn initscript
    # Description: Unicorn is an HTTP server for Rack application
    ### END INIT INFO

    # based on http://gist.github.com/308216 by http://github.com/mguterl
    @@ -15,6 +15,9 @@
    ##
    ## RAILS_ENV=production
    ## RAILS_ROOT=/var/apps/www/my_app/current
    ## PID=$RAILS_ROOT/tmp/unicorn.pid
    ## START_CMD="bundle exec unicorn"
    ## USER="www-data"

    set -e

  6. troex revised this gist Dec 1, 2011. 1 changed file with 27 additions and 17 deletions.
    44 changes: 27 additions & 17 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: thin
    # Required-Start: $local_fs $remote_fs
    # Required-Start: $local_fs $remote_fs mysql
    # Required-Stop: $local_fs $remote_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    @@ -30,56 +30,65 @@ cmd () {

    case $1 in
    start)
    sig 0 && echo >&2 "Already running" && exit 0
    sig 0 && echo >&2 "Already running" && return
    echo "Starting"
    $CMD
    `$CMD`
    ;;
    stop)
    sig QUIT && echo "Stopping" && exit 0
    sig QUIT && echo "Stopping" && return
    echo >&2 "Not running"
    ;;
    force-stop)
    sig TERM && echo "Forcing a stop" && exit 0
    sig TERM && echo "Forcing a stop" && return
    echo >&2 "Not running"
    ;;
    restart|reload)
    sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && exit 0
    sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && return
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    $CMD
    ;;
    upgrade)
    sig USR2 && echo Upgraded && exit 0
    sig USR2 && echo Upgraded && return
    echo >&2 "Couldn't upgrade, starting '$CMD' instead"
    $CMD
    ;;
    rotate)
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    ;;
    *)
    sig USR1 && echo rotated logs OK && return
    echo >&2 "Couldn't rotate logs" && return
    ;;
    status)
    sig 0 && echo >&2 "Already running" && return
    echo >&2 "Not running" && return
    ;;
    *)
    echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
    exit 1
    return
    ;;
    esac
    }

    setup () {
    echo -n "$RAILS_ROOT: "
    cd $RAILS_ROOT || exit 1
    if [ -z $PID ]; then

    if [ -z "$PID" ]; then
    PID=$RAILS_ROOT/tmp/pids/unicorn.pid
    fi

    export PID
    export OLD_PID="$PID.oldbin"
    export RAILS_ROOT

    if [ -z $START_CMD ]; then
    if [ -z "$START_CMD" ]; then
    START_CMD="bundle exec unicorn_rails"
    fi

    CMD="$START_CMD -c $UNICORN_CONFIG -E $RAILS_ENV -D"
    echo $CMD
    exit

    if [ "$USER" != `whoami` ]; then
    CMD="sudo -u $USER -- $CMD"
    fi
    export CMD
    #echo $CMD
    }

    start_stop () {
    @@ -103,6 +112,7 @@ start_stop () {

    # run the start/stop/etc command
    cmd $1
    unset PID
    done
    fi
    }
  7. troex revised this gist Nov 30, 2011. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -73,8 +73,13 @@ setup () {
    export OLD_PID="$PID.oldbin"
    export RAILS_ROOT

    CMD="bundle exec unicorn_rails -c $UNICORN_CONFIG -E $RAILS_ENV -D"
    #echo $CMD
    if [ -z $START_CMD ]; then
    START_CMD="bundle exec unicorn_rails"
    fi

    CMD="$START_CMD -c $UNICORN_CONFIG -E $RAILS_ENV -D"
    echo $CMD
    exit
    }

    start_stop () {
  8. troex revised this gist Sep 2, 2011. 1 changed file with 88 additions and 58 deletions.
    146 changes: 88 additions & 58 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -1,76 +1,106 @@
    #!/bin/sh

    ### BEGIN INIT INFO
    # Provides: unicorn-kk
    # Provides: thin
    # Required-Start: $local_fs $remote_fs
    # Required-Stop: $local_fs $remote_fs
    # Should-Start: mysql
    # Should-Stop: mysql
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: unicorn
    # Description: unicorn
    # Short-Description: thin initscript
    # Description: thin
    ### END INIT INFO

    # based on http://gist.github.com/308216 by http://github.com/mguterl
    #
    ## A sample /etc/unicorn/my_app.conf
    ##
    ## RAILS_ENV=production
    ## RAILS_ROOT=/var/apps/www/my_app/current

    set -u
    set -e
    # Example init script, this can be used with nginx, too,
    # since nginx and unicorn accept the same signals

    # Feel free to change any of the following variables for your app:
    APP_ROOT=/var/www/app/current
    PID=/var/www/app/shared/pids/unicorn.pid
    ENV=production
    CMD="/usr/bin/unicorn_rails -D -E $ENV -c config/unicorn.rb"
    sig () {
    test -s "$PID" && kill -$1 `cat "$PID"`
    }

    old_pid="$PID.oldbin"
    oldsig () {
    test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"`
    }

    cd $APP_ROOT || exit 1
    cmd () {

    sig () {
    test -s "$PID" && kill -$1 `cat $PID`
    case $1 in
    start)
    sig 0 && echo >&2 "Already running" && exit 0
    echo "Starting"
    $CMD
    ;;
    stop)
    sig QUIT && echo "Stopping" && exit 0
    echo >&2 "Not running"
    ;;
    force-stop)
    sig TERM && echo "Forcing a stop" && exit 0
    echo >&2 "Not running"
    ;;
    restart|reload)
    sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && exit 0
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    $CMD
    ;;
    upgrade)
    sig USR2 && echo Upgraded && exit 0
    echo >&2 "Couldn't upgrade, starting '$CMD' instead"
    $CMD
    ;;
    rotate)
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    ;;
    *)
    echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
    exit 1
    ;;
    esac
    }

    oldsig () {
    test -s $old_pid && kill -$1 `cat $old_pid`
    setup () {
    echo -n "$RAILS_ROOT: "
    cd $RAILS_ROOT || exit 1
    if [ -z $PID ]; then
    PID=$RAILS_ROOT/tmp/pids/unicorn.pid
    fi
    export PID
    export OLD_PID="$PID.oldbin"
    export RAILS_ROOT

    CMD="bundle exec unicorn_rails -c $UNICORN_CONFIG -E $RAILS_ENV -D"
    #echo $CMD
    }

    start_stop () {

    # either run the start/stop/reload/etc command for every config under /etc/unicorn
    # or just do it for a specific one

    # $1 contains the start/stop/etc command
    # $2 if it exists, should be the specific config we want to act on
    if [ -f "/etc/unicorn/$2.conf" ]; then
    . /etc/unicorn/$2.conf
    export UNICORN_CONFIG="/etc/unicorn/$2.unicorn.rb"
    setup
    cmd $1
    else
    for CONFIG in /etc/unicorn/*.conf; do
    # import the variables
    export UNICORN_CONFIG=`echo ${CONFIG} | sed 's/conf/unicorn.rb/'`
    . $CONFIG
    setup

    # run the start/stop/etc command
    cmd $1
    done
    fi
    }

    case $1 in
    start)
    sig 0 && echo >&2 "Already running" && exit 0
    $CMD
    echo >&2 "Started"
    ;;
    stop)
    sig QUIT && exit 0
    echo >&2 "Not running"
    ;;
    force-stop)
    sig TERM && exit 0
    echo >&2 "Not running"
    ;;
    reload)
    sig HUP && echo reloaded OK && exit 0
    echo >&2 "Couldn't reload"
    #$CMD
    ;;
    restart)
    sig USR2
    echo >&2 "Starting new master, waiting 10 sec..."
    sleep 10
    echo -n >&2 "Quiting old master... "
    oldsig QUIT
    echo >&2 "done"
    #echo >&2 "Couldn't upgrade, starting '$CMD' instead"
    #$CMD
    ;;
    rotate)
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    ;;
    *)
    echo >&2 "Usage: $0 <start|stop|reload|restart|rotate|force-stop>"
    exit 1
    ;;
    esac
    ARGS="$1 $2"
    start_stop $ARGS
  9. troex revised this gist Apr 4, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,12 @@
    # Provides: unicorn-kk
    # Required-Start: $local_fs $remote_fs
    # Required-Stop: $local_fs $remote_fs
    # Should-Start: mysql memcached sphinx-kk
    # Should-Stop: mysql memcached sphinx-kk
    # Should-Start: mysql
    # Should-Stop: mysql
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: unicorn kk
    # Description: unicorn kk
    # Short-Description: unicorn
    # Description: unicorn
    ### END INIT INFO


  10. troex created this gist Apr 4, 2011.
    76 changes: 76 additions & 0 deletions unicorn.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/bin/sh

    ### BEGIN INIT INFO
    # Provides: unicorn-kk
    # Required-Start: $local_fs $remote_fs
    # Required-Stop: $local_fs $remote_fs
    # Should-Start: mysql memcached sphinx-kk
    # Should-Stop: mysql memcached sphinx-kk
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: unicorn kk
    # Description: unicorn kk
    ### END INIT INFO


    set -u
    set -e
    # Example init script, this can be used with nginx, too,
    # since nginx and unicorn accept the same signals

    # Feel free to change any of the following variables for your app:
    APP_ROOT=/var/www/app/current
    PID=/var/www/app/shared/pids/unicorn.pid
    ENV=production
    CMD="/usr/bin/unicorn_rails -D -E $ENV -c config/unicorn.rb"

    old_pid="$PID.oldbin"

    cd $APP_ROOT || exit 1

    sig () {
    test -s "$PID" && kill -$1 `cat $PID`
    }

    oldsig () {
    test -s $old_pid && kill -$1 `cat $old_pid`
    }

    case $1 in
    start)
    sig 0 && echo >&2 "Already running" && exit 0
    $CMD
    echo >&2 "Started"
    ;;
    stop)
    sig QUIT && exit 0
    echo >&2 "Not running"
    ;;
    force-stop)
    sig TERM && exit 0
    echo >&2 "Not running"
    ;;
    reload)
    sig HUP && echo reloaded OK && exit 0
    echo >&2 "Couldn't reload"
    #$CMD
    ;;
    restart)
    sig USR2
    echo >&2 "Starting new master, waiting 10 sec..."
    sleep 10
    echo -n >&2 "Quiting old master... "
    oldsig QUIT
    echo >&2 "done"
    #echo >&2 "Couldn't upgrade, starting '$CMD' instead"
    #$CMD
    ;;
    rotate)
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    ;;
    *)
    echo >&2 "Usage: $0 <start|stop|reload|restart|rotate|force-stop>"
    exit 1
    ;;
    esac