Skip to content

Instantly share code, notes, and snippets.

@mswart
Created August 21, 2012 09:10
Show Gist options
  • Save mswart/3413790 to your computer and use it in GitHub Desktop.
Save mswart/3413790 to your computer and use it in GitHub Desktop.
Init.d-Script for unicorn with bundle and rvm
UNICORN_USER=www-data
UNICORN_GROUP=www-data
BUNDLE_GEMFILE=/var/www/current/Gemfile
RVM_EXE=/usr/local/rvm/bin/rvm
RAILS_ROOT=/var/www/current/
RAILS_ENV=production
RAILS_VERSION=ree
UNICORN_CONFIG="${RAILS_ROOT}/config/unicorn.rb"
UNICORN_PIDFILE=${RAILS_ROOT}/tmp/pids/unicorn.pid
RAILS_EXE=/usr/local/rvm/rubies/ree-1.8.7-2012.02/bin/ruby
#! /bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: unicorn server init.d script
### END INIT INFO
set -e
umask 022
if test -f /etc/default/unicorn; then
. /etc/default/unicorn
fi
. /lib/lsb/init-functions
cd $RAILS_ROOT
export BUNDLE_GEMFILE
case "$1" in
start)
log_daemon_msg "Starting Unicorn Webserver" "unicorn_rails" || true
start-stop-daemon --start --chuid $UNICORN_USER:$UNICORN_GROUP --chdir $RAILS_ROOT --pidfile $UNICORN_PIDFILE --exec $RVM_EXE -- $RUBY_VERSION do bundle exec unicorn_rails -c $UNICORN_CONFIG -E $RAILS_ENV -D
log_end_msg $? || true
;;
stop)
log_daemon_msg "Stopping Unicorn web server" "unicorn_rails" || true
if start-stop-daemon --stop --quiet --oknodo --pidfile "${UNICORN_PIDFILE}"; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
restart)
log_daemon_msg "Restarting Unicorn web server" "unicorn_rails" || true
kill -s USR2 `cat $UNICORN_PIDFILE`
log_end_msg $? || true
;;
status)
status_of_proc -p $UNICORN_PIDFILE $RAILS_EXE unicorn_rails && exit 0 || exit $?
;;
*)
log_action_msg "Usage: /etc/init.d/unicorn {start|stop|restart|status}" || true
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment