• Download Gist
unicorn init script for multiple applicagtions
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#!/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 agirorn@gmail.com 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.