public
Last active — forked from mguterl/gist:308216

/etc/init.d script for unicorn

  • Download Gist
unicorn
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
#!/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
#
## 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
 
sig () {
test -s "$PID" && kill -$1 `cat "$PID"`
}
 
oldsig () {
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"`
}
 
cmd () {
 
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting"
$CMD
;;
stop)
sig QUIT && echo "Stopping" && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && echo "Forcing a stop" && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
$CMD
;;
upgrade)
sig USR2 && echo Upgraded && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
}
 
setup () {
 
echo -n "$RAILS_ROOT: "
cd $RAILS_ROOT || exit 1
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID="$PID.oldbin"
 
CMD="/usr/bin/unicorn_rails -c 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

Thanks for this!

On 10 May 2011 23:53, zaius
reply@reply.github.com
wrote:

Thanks for this!

:)

Jay Caines-Gooby
http://jay.gooby.org
jay@gooby.org
+44 (0)7956 182625
twitter, skype & aim: jaygooby
gtalk: jaygooby@gmail.com

Very cool.

Just one issue: How to use with RVM and different gemsets?

Thanks!!!

@fernandokosh -

I actually made a small change to use with rvm... and with current unicorn instead of unicorn_rails. The gist is here.

Very elegant your solution.
Thanks @canweriotnow.

Nice script, I've made another implementation with bundle exec and unicorn.rb in /etc/unicorn trx's unicorn init

troex, nice implementation with bundle exec and unicorn.rb.

@fernandokosh thanks! I'll keep updating and make it more flexible also some examples are needed.

troex, and how about run with per projects gemsets? Imagine you have the project in /home/user/projects/app1. We need to activate /home/user/projects/app1/.rvmrc to correct load the gemset. I'm trying to do it, but...

Any idea?

Thanks

@fernandokosh I haven't used RVM so I don't what it needs to be activated, but in my implementation you can define your own START_CMD variable. Keep asking in my gist please.

@fernandokosh i have modified that script to support for per project specific gemsets. You can check it here: https://gist.github.com/2623205

@fernandokosh i have modified the script for supporting for RVM on Mac OS X https://gist.github.com/3447365

One thing, when sending USR2 on upgrade, you still need to verify the new process is working and send QUIT to the .oldbin unicorn master.
http://unicorn.bogomips.org/SIGNALS.html

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.