Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save agirorn/566592 to your computer and use it in GitHub Desktop.
Save agirorn/566592 to your computer and use it in GitHub Desktop.
#!/bin/bash
### BEGIN INIT INFO
# Provides: APPLICATION
# Required-Start: $all
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the APPLICATION unicorns at boot
# Description: Enable APPLICATION at boot time.
### END INIT INFO
#
# init.d script for single or multiple unicorn installations. Expects at least one .conf
# file in /etc/unicorn
#
# Modified by [email protected] http://github.com/agirorn
# based on http://gist.github.com/504875 by http://github.com/jaygooby and
# http://gist.github.com/420880 from http://blog.kiskolabs.com/post/722322392/unicorn-init-scripts
#
## 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
sig () {
test -s $PID && kill -s $1 `cat $PID`
}
function setup_rvm() {
# LOAD RVM IF IT EXISTS
if [ -f '/usr/local/lib/rvm' ]; then
. '/usr/local/lib/rvm'
fi
}
function setup() {
setup_rvm
COMMAND=$1
if [ $2 ]; then
UNICORN=/etc/unicorn/$2.conf
start_stop
else
for UNICORN in /etc/unicorn/*.conf; do
# import the variables
start_stop
# run the start/stop/etc command
#cmd $1
done
fi
}
function start_stop() {
. $UNICORN
PID=$RAILS_ROOT/tmp/pids/unicorn.pid
OLD_PID=$PID.oldbin
CMD=" unicorn_rails -D -E $RAILS_ENV -c config/unicorn.rb"
CD="cd $RAILS_ROOT"
echo $UNICORN
case $COMMAND in
start)
if [ -s $PID ] ; then
echo >&2 "Already running"
else
echo "Starting" && $CD && $CMD
fi
;;
stop)
if [ -s $PID ] ; then
echo "Stopping" && kill -s QUIT `cat $PID`
else
echo >&2 "Not running"
fi
;;
force-stop)
if [ -s $PID ] ; then
echo "Forcing a stop" && kill -s TERM `cat $PID`
else
echo >&2 "Not running"
fi
;;
restart|reload)
echo "Restarting"
if [ -s $PID ] ; then
kill -s HUP `cat $PID`
else
$CD && $CMD
fi
;;
upgrade)
if [ -s $PID ] ; then
echo "Upgraded" && kill -s USR2 `cat $PID`
else
echo >&2 "Not running"
fi
;;
rotate)
if [ -s $PID ] ; then
echo "rotated logs OK" && kill -s USR1 `cat $PID`
else
echo >&2 "Not running"
fi
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
}
setup $1 $2
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment