Created
September 26, 2014 03:39
-
-
Save hSATAC/3fc3a46da5da9fa9b310 to your computer and use it in GitHub Desktop.
/etc/init.d/unicorn Unicorn init script for multiple apps
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 | |
### BEGIN INIT INFO | |
# Provides: unicorn | |
# Required-Start: $all | |
# Required-Stop: $network $local_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start the unicorns at boot | |
# Description: Enable at boot time. | |
### END INIT INFO | |
# This is /etc/init.d/unicorn_init (without .sh) | |
# init.d script for single or multiple unicorn installations. Expects at least one .conf | |
# file in /etc/unicorn | |
# | |
# Forked from https://gist.github.com/zewelor/2623205 | |
# based on modified version by http://github.com/killercup | |
# which based on modified version by jay@gooby.org http://github.com/jaygooby | |
# which is 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 | |
## UNICORN="/usr/local/rvm/bin/<WRAPPED_NAME>" #see rvm wrapper above | |
## APP_USER="some_user" | |
## UNICORN_CONFIG="/var/apps/www/my_app/shared/config/unicorn.rb" | |
# | |
# 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 my_app | |
export rvmsudo_secure_path=1 | |
RVM_FILE_TO_SOURCE="/home/railsapp/.rvm/scripts/rvm" | |
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 0 | |
eval $CMD | |
echo "Starting" | |
;; | |
stop) | |
sig QUIT && echo "Stopping" && return 0 | |
echo >&2 "Not running" | |
;; | |
force-stop) | |
sig TERM && echo "Forcing a stop" && return 0 | |
echo >&2 "Not running" | |
;; | |
restart|reload) | |
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && return 0 | |
echo >&2 "Couldn't reload, starting '$CMD' instead" | |
eval $CMD | |
;; | |
upgrade) | |
sig USR2 && echo Upgraded && return 0 | |
echo >&2 "Couldn't upgrade, starting '$CMD' instead" | |
eval $CMD | |
;; | |
rotate) | |
sig USR1 && echo rotated logs OK && return 0 | |
echo >&2 "Couldn't rotate logs" && return 1 | |
;; | |
*) | |
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>" | |
exit 1 | |
;; | |
esac | |
} | |
setup () { | |
CONFIG=$1 | |
export APP_NAME=`basename $1|cut -f 1 -d '.'` | |
echo "Launching ${APP_NAME}" | |
if [ -z $APP_USER ]; then | |
if [ `/bin/egrep -i "^${APP_NAME}" /etc/passwd|wc -l` -eq 1 ]; then | |
export APP_USER=$APP_NAME | |
else | |
echo "The is no user defined in ${CONFIG}, and the is no user in the system called ${APP_NAME}" | |
return 1 | |
fi | |
fi | |
if [ -z $RAILS_ROOT ]; then | |
if [ -d "/home/${APP_USER}/www/current" ]; then | |
export RAILS_ROOT="/home/${APP_USER}/www/current" | |
else | |
echo "Rails root is not defined in ${CONFIG}" | |
return 1 | |
fi | |
fi | |
if [ -z $RAILS_ENV ]; then | |
export RAILS_ENV="production" | |
fi | |
# If unicorn binary was not defined in config | |
if [ -z "$UNICORN" ]; then | |
UNICORN="unicorn_rails" | |
fi | |
# If unicorn config was not defined in config | |
if [ -z $UNICORN_CONFIG ]; then | |
UNICORN_CONFIG=${RAILS_ROOT}/config/unicorn/${RAILS_ENV}.rb | |
fi | |
cd $RAILS_ROOT || return 1 | |
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid | |
export OLD_PID="$PID.oldbin" | |
CMD="rvmsudo -u ${APP_USER} ${UNICORN} -E ${RAILS_ENV} -c ${UNICORN_CONFIG} -D" | |
return 0 | |
} | |
# 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 | |
source $RVM_FILE_TO_SOURCE | |
if [ $2 ]; then | |
. /etc/unicorn/$2.conf | |
setup "/etc/unicorn/$2.conf" | |
if [ $? -eq 1 ]; then | |
exit | |
fi | |
cmd $1 | |
else | |
for CONFIG in /etc/unicorn/*.conf; do | |
# clean variables from prev configs | |
unset APP_USER | |
unset RAILS_ROOT | |
unset RAILS_ENV | |
unset UNICORN | |
unset UNICORN_CONFIG | |
# import the variables | |
. $CONFIG | |
setup $CONFIG | |
if [ $? -eq 1 ]; then | |
continue | |
fi | |
# run the start/stop/etc command | |
cmd $1 | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment