Created
September 6, 2012 11:09
-
-
Save terut/3654895 to your computer and use it in GitHub Desktop.
Unicorn multi init.
This file contains hidden or 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 | |
# | |
# chkconfig: - 88 12 | |
# | |
# init.d script for single or multiple unicorn installations. Expects at least one .env | |
# file in /etc/unicorn | |
# | |
## A sample /etc/unicorn/production.env | |
## | |
## 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/*.env | |
# | |
# /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/production.conf | |
set -e | |
ENV_DIR=/etc/unicorn | |
usage_exit() { | |
echo >&2 "Usage: $0 [-c $ENV_DIR/my_app.env] <start|stop|restart|upgrade|rotate|force-stop>" | |
exit 1 | |
} | |
sig () { | |
test -s "$PID" && kill -$1 `cat "$PID"` | |
} | |
oldsig () { | |
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"` | |
} | |
start () { | |
su $USER -c "$USER_ENV && $CMD" | |
} | |
cmd () { | |
case $1 in | |
start) | |
sig 0 && echo >&2 "Already running" && exit 0 | |
echo "Starting" | |
start | |
;; | |
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) | |
# preload_appの設定によってはHUPでよい | |
# oldのQUITはunicorn.rbにまかせる | |
# sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && exit 0 | |
sig USR2 && echo "Killing old master" `cat $OLD_PID` && exit 0 | |
echo >&2 "Couldn't reload, starting '$CMD' instead" | |
start | |
;; | |
upgrade) | |
# これ使う機会ない気がする | |
sig USR2 && echo Upgraded && exit 0 | |
echo >&2 "Couldn't upgrade, starting '$CMD' instead" | |
start | |
;; | |
rotate) | |
sig USR1 && echo rotated logs OK && exit 0 | |
echo >&2 "Couldn't rotate logs" && exit 1 | |
;; | |
*) | |
usage_exit | |
;; | |
esac | |
} | |
process_check () { | |
if [ -s $PID ] && ! kill -0 `cat "$PID"` ; then | |
rm $PID | |
fi | |
} | |
setup () { | |
echo -n "$RAILS_ROOT: " | |
cd $RAILS_ROOT || exit 1 | |
export OLD_PID="$PID.oldbin" | |
process_check | |
#CMD="/usr/bin/unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D" | |
CMD="cd $RAILS_ROOT && BUNDLE_GEMFILE=$RAILS_ROOT/Gemfile bundle exec 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 [ $ENV_FILE ]; then | |
# import valiables | |
. $ENV_FILE | |
setup | |
cmd $1 | |
else | |
for ENV_FILE in $ENV_DIR/*.env; do | |
# import the variables | |
. $ENV_FILE | |
setup | |
# run the start/stop/etc command | |
cmd $1 | |
done | |
fi | |
} | |
while getopts c: OPT | |
do | |
case $OPT in | |
c) | |
ENV_FILE=$OPTARG | |
;; | |
h) | |
usage_exit | |
;; | |
*) | |
usage_exit | |
;; | |
esac | |
done | |
# $1 に restart等を持ってくる | |
shift $(( $OPTIND - 1 )) | |
ARGS="$1" | |
start_stop $ARGS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It isn't works. because
cmd()
is return exit 0.