Created
December 5, 2011 07:12
-
-
Save pacojp/1432656 to your computer and use it in GitHub Desktop.
unicorn init script
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 | |
### BEGIN INIT INFO | |
# Provides: unicorn | |
# Required-Start: $local_fs $remote_fs mysql | |
# Required-Stop: $local_fs $remote_fs | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: unicorn initscript | |
# Description: Unicorn is an HTTP server for Rack application | |
### END INIT INFO | |
# 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 | |
## RAILS_PORT=9000 | |
## PID=$RAILS_ROOT/tmp/unicorn.pid | |
## START_CMD="bundle exec unicorn" | |
## USER="www-data" | |
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" && return | |
echo "Starting" | |
echo $CMD | |
`$CMD` | |
;; | |
stop) | |
sig QUIT && echo "Stopping" && return | |
echo >&2 "Not running" | |
;; | |
force-stop) | |
sig TERM && echo "Forcing a stop" && return | |
echo >&2 "Not running" | |
;; | |
restart|reload) | |
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && return | |
echo >&2 "Couldn't reload, starting '$CMD' instead" | |
$CMD | |
;; | |
upgrade) | |
sig USR2 && echo Upgraded && return | |
echo >&2 "Couldn't upgrade, starting '$CMD' instead" | |
$CMD | |
;; | |
rotate) | |
sig USR1 && echo rotated logs OK && return | |
echo >&2 "Couldn't rotate logs" && return | |
;; | |
status) | |
sig 0 && echo >&2 "Already running" && return | |
echo >&2 "Not running" && return | |
;; | |
*) | |
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>" | |
return | |
;; | |
esac | |
} | |
setup () { | |
echo -n "$RAILS_ROOT: " | |
cd $RAILS_ROOT || exit 1 | |
if [ -z "$PID" ]; then | |
PID=$RAILS_ROOT/tmp/pids/unicorn.pid | |
fi | |
export PID | |
export OLD_PID="$PID.oldbin" | |
export RAILS_ROOT | |
export RAILS_PORT | |
if [ -z "$START_CMD" ]; then | |
START_CMD="bundle exec unicorn_rails" | |
fi | |
CMD="cd $RAILS_ROOT && $START_CMD -c $UNICORN_CONFIG -E $RAILS_ENV -p $RAILS_PORT -D" | |
if [ "$USER" != `whoami` ]; then | |
CMD="sudo -i -u $USER -H -- $CMD" | |
fi | |
export CMD | |
#echo $CMD | |
} | |
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 [ -f "/etc/unicorn/$2.conf" ]; then | |
. /etc/unicorn/$2.conf | |
export UNICORN_CONFIG="/etc/unicorn/$2.unicorn.rb" | |
setup | |
cmd $1 | |
else | |
for CONFIG in /etc/unicorn/*.conf; do | |
# import the variables | |
export UNICORN_CONFIG=`echo ${CONFIG} | sed 's/conf/unicorn.rb/'` | |
. $CONFIG | |
setup | |
# run the start/stop/etc command | |
cmd $1 | |
unset PID | |
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