Skip to content

Instantly share code, notes, and snippets.

@glebm
Created August 3, 2012 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glebm/3242512 to your computer and use it in GitHub Desktop.
Save glebm/3242512 to your computer and use it in GitHub Desktop.
Unicorn /etc/init.d script for use with bundler
#!/bin/sh
#
# init.d script for single or multiple unicorn installations. Expects at least one .conf
# file in /etc/unicorn
#
# Modified by jay@gooby.org http://github.com/jaygooby
# based on http://gist.github.com/308216 by http://github.com/mguterl
# Updated by glebm for newer unicorn and to use with bundle exec
#
## A sample /etc/unicorn/my_app.conf
##
## RAILS_ENV=production
## RAILS_ROOT=/var/apps/www/my_app/current
#
# This configures a unicorn master for your app at /var/apps/www/my_app/current running in
# production mode. It will read config/unicorn.rb for further set up.
#
# You should ensure different ports or sockets are set in each config/unicorn.rb if
# you are running more than one master concurrently.
#
# If you call this script without any config parameters, it will attempt to run the
# init command for all your unicorn configurations listed in /etc/unicorn/*.conf
#
# /etc/init.d/unicorn start # starts all unicorns
#
# If you specify a particular config, it will only operate on that one
#
# /etc/init.d/unicorn start /etc/unicorn/my_app.conf
set -e
cmd () {
cd $RAILS_ROOT;
case $1 in
start)
start-stop-daemon -d $RAILS_ROOT --start --quiet --pidfile $PID \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "Starting with config: $RAILS_ROOT/config/unicorn.rb"
;;
stop)
start-stop-daemon -d $RAILS_ROOT --stop --quiet --pidfile $PID || true
echo "Stopping with config: $RAILS_ROOT/config/unicorn.rb"
;;
restart|reload)
start-stop-daemon -d $RAILS_ROOT --stop --quiet --pidfile $PID || true
echo "Stopping with config: $RAILS_ROOT/config/unicorn.rb"
sleep 1
start-stop-daemon -d $RAILS_ROOT --start --quiet --pidfile $PID \
--exec $DAEMON -- $DAEMON_OPTS || true
echo "Starting with config: $RAILS_ROOT/config/unicorn.rb"
;;
*)
echo >&2 "Usage: $0 <start|stop|restart>"
exit 1
;;
esac
}
setup () {
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID="$PID.oldbin"
DAEMON="`which bundle` exec unicorn_rails"
DAEMON_OPTS="-c $RAILS_ROOT/config/unicorn.rb -E $RAILS_ENV -D"
}
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 [ $2 ]; then
. $2
setup
cmd $1
else
for CONFIG in /etc/unicorn/*.conf; do
# import the variables
. $CONFIG
setup
# run the start/stop/etc command
cmd $1
done
fi
}
ARGS="$1 $2"
start_stop $ARGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment