Created
October 7, 2012 10:19
-
-
Save jalaziz/3847776 to your computer and use it in GitHub Desktop.
init.d script for unicorn with RVM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# init.d script for single or multiple unicorn installations. Expects at least one .conf | |
# file in /etc/unicorn | |
# | |
# based on git://gist.github.com/504875.git [http://github.com/jaygooby] | |
# support for RVM on Mac OS X | |
# Modified by JeremyWei<http://github.com/jeremy> | |
# | |
# run the code below, you will get a script named bootup_unicorn in ~/.rvm/bin. | |
# then set a env variable named UNICORN in config file /etc/unicorn/my_app.conf | |
# $ rvm wrapper ruby-1.9.3-p194 bootup unicorn | |
# | |
## A sample /etc/unicorn/my_app.conf | |
## | |
## APP_ENV=production | |
## APP_ROOT=/var/apps/www/my_app/current | |
## UNICORN=~/.rvm/bin/bootup_unicorn #path to bootup_unicorn | |
# | |
# 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 app, it will only operate on that one | |
# | |
# /etc/init.d/unicorn start my_app | |
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) | |
sig QUIT && echo "Stopping" && sleep 5 && echo "Starting" | |
$CMD | |
;; | |
reload) | |
sig USR2 && sleep 5 && sig 0 && echo "New master started...Killing old master" && oldsig QUIT && echo Reloaded && exit 0 | |
echo >&2 "Couldn't reload, 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|reload|rotate|force-stop>" | |
exit 1 | |
;; | |
esac | |
} | |
setup () { | |
echo -n "$APP_ROOT: " | |
cd $APP_ROOT || exit 1 | |
mkdir -p /var/run/unicorn # Make sure the PID directory exists | |
mkdir -p /var/log/unicorn # Make sure the log directory exists | |
export PID=/var/run/unicorn/$1.pid | |
export OLD_PID="$PID.oldbin" | |
CMD="$UNICORN -c config/unicorn.rb -E $APP_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 | |
. /etc/unicorn/$2.conf | |
setup $2 | |
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