#!/bin/sh set -e TIMEOUT=60 export RAILS_ENV=production ACTION="$1" if [ -z "$ACTION" ]; then echo "usage: $0 start|stop|restart" exit 1 fi app_server_start() { ruby -S bundle exec unicorn_rails \ --config-file config/unicorn.rb \ --env "$RAILS_ENV" \ --daemonize } app_server_stop() { kill -s QUIT $(cat tmp/pids/unicorn.pid) } app_server_restart() { kill -s USR2 $(cat tmp/pids/unicorn.pid) sleep 5 kill -s QUIT $(cat tmp/pids/unicorn.pid.oldbin) } do_start() { app_server_start } do_stop() { app_server_stop || stop_via_pid_file tmp/pids/unicorn.pid } do_restart() { app_server_restart } stop_via_pid_file() { for pidfile in $@; do if [ -e "$pidfile" ]; then pid=$(cat $pidfile) echo "Sentign TERM signal to stop $pid ..." kill -TERM "$pid" rm -f $pidfile fi done } do_running() { pids=$(sed "s/.*/& /" tmp/pids/unicorn.pid 2>/dev/null | tr -d '\n') kill -0 $pids > /dev/null 2>&1 } case "$ACTION" in start|stop) do_$ACTION ;; run) do_start echo "=> Running in production mode. Hit ctrl-C to stop." trap do_stop INT TERM tail -n 0 -f log/production.log || true ;; restart) do_restart ;; running) do_running ;; *) echo "usage: $0 start|stop|restart" exit 1 ;; esac