Created
September 15, 2012 12:11
-
-
Save Foxandxss/3727516 to your computer and use it in GitHub Desktop.
Thin init.d script
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
RAILS_ENV=production | |
RAILS_ROOT=/srv/http/ruby/thin | |
CMD="/srv/http/.rvm/gems/ruby-1.9.3-p194/bin/thin start -C current/config/thin.yml" |
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/bash | |
# | |
# Modified by Foxandxss@gmail.com http://github.com/Foxandxss | |
# Modified by ilgrim@gmail.com http://bitbucket.org/devlon | |
# Modified by jay@gooby.org http://github.com/jaygooby | |
# based on http://gist.github.com/308216 by http://github.com/mguterl | |
# | |
## | |
# Configure /etc/default/unicorn with these vars: | |
## | |
## RAILS_ENV=production | |
## RAILS_ROOT=/var/apps/ww | |
# | |
# 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 | |
## Load general settings | |
. /etc/default/thin | |
sig () { | |
test -s "$PID" && kill -$1 $(cat "$PID") | |
} | |
oldsig () { | |
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"` | |
} | |
cmd () { | |
case $1 in | |
start) | |
if [ ! -f $PID ]; then | |
echo -e " >> Launching" | |
$CMD | |
echo -e " :: PID:\t\t [ $(cat $PID) ]" ; | |
else | |
echo -e >&2 " >> Already running. Aborting" | |
fi | |
;; | |
stop) | |
if [ -f $PID ]; then | |
echo -e " >> Stopping" | |
sig QUIT | |
else | |
echo -e >&2 " >> Not running" | |
fi | |
;; | |
force-stop) | |
if [ -f $PID ]; then | |
echo " >> Forcing a stop" | |
sig TERM | |
else | |
echo -e >&2 " >> Not running" | |
fi | |
;; | |
restart|reload) | |
if [ -f $PID ]; then | |
echo -e " >> Restarting" | |
start_stop stop $2; sleep 2 && start_stop start $2 | |
fi | |
;; | |
*) | |
echo >&2 "Usage: $0 <start|stop|restart|reload>" | |
exit 1 | |
;; | |
esac | |
} | |
setup () { | |
if [ -d $RAILS_ROOT/$1 ]; then | |
cd $RAILS_ROOT/$1 || exit 1 | |
echo -e ":: Setting up APP:\t [ $1 ]" | |
echo -e " > Path:\t\t [ $(pwd) ]" | |
gemset="/srv/http/.rvm/environments/ruby-1.9.3-p194@$1" | |
echo -e " > Sourcing to:\t [ $gemset ]" | |
source $gemset | |
# since the pid name is different in every project, we need to get the name first | |
pidid=`grep "port:" $RAILS_ROOT/$1/current/config/thin.yml | sed 's/.*: //g'` | |
export PID=$RAILS_ROOT/$1/current/tmp/pids/thin.$pidid.pid | |
export OLD_PID="$PID.oldbin" | |
fi | |
} | |
start_stop () { | |
# $1 contains the start/stop/etc command | |
# $2 if it exists, should be the specific APP name we want to act on | |
if [ ! -z $2 ]; then | |
if [ -d $RAILS_ROOT/$2 ]; then | |
setup $2 | |
cmd $1 $2 | |
echo "" | |
else | |
echo -e ">> Environment [ $2 ] does not exist" | |
fi | |
else | |
for APP in $(ls $RAILS_ROOT) | |
do | |
setup $APP | |
cmd $1 $APP | |
echo "" | |
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