Skip to content

How to use it with upstart (`sudo service unicorn start|stop|restart`)

Lucas Caton edited this page Aug 15, 2016 · 1 revision

File /etc/init.d/unicorn:

#!/bin/sh
set -e
. /etc/environment

USER="ubuntu"
APP_ROOT=/var/www/unicorn/current
RBENV_ROOT="/home/$USER/.rbenv"
PATH="$RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH"
PID=/var/www/unicorn/shared/pids/unicorn.pid
CMD="bundle exec unicorn -c config/unicorn.rb -E $RAILS_ENV -D"

action="$1"
set -u

cd $APP_ROOT || exit 1

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

case $action in
start)
        sig 0 && echo >&2 "Already running" && exit 0
        $CMD
        ;;
stop)
        kill -s QUIT `cat /var/www/unicorn/shared/pids/unicorn.pid`
        ;;
restart|reload|upgrade)
        sig 0 || (echo >&2 "Unicorn was not running" && $CMD && exit 0)

        kill -s USR2 `cat /var/www/unicorn/shared/pids/unicorn.pid`
        sleep 3
        kill -s QUIT `cat /var/www/unicorn/shared/pids/unicorn.pid.oldbin`
        ;;
*)
        echo >&2 "Usage: $0 <start|stop|restart>"
        exit 1
        ;;
esac

Usage:

  • sudo service unicorn start
  • sudo service unicorn stop
  • sudo service unicorn restart