Last active
December 25, 2015 11:59
-
-
Save dchentech/6973600 to your computer and use it in GitHub Desktop.
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 | |
# | |
# https://gist.github.com/mvj3/6973600 | |
# | |
# unicorn - init.d script for single or multiple unicorn installations. Expects at least one .conf | |
# chkconfig: - 85 15 | |
# description: Unicorn is an HTTP server for Rack applications designed to only serve fast clients on low-latency, | |
# high-bandwidth connections and take advantage of features in Unix/Unix-like kernels | |
# processname: unicorn | |
# config: /etc/unicorn/*.conf | |
# | |
# | |
## A sample /etc/unicorn/my_app.conf | |
## | |
## ENV=production | |
## APP_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/*.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 | |
### BEGIN INIT INFO | |
# Provides: unicorn | |
# Required-Start: $network $local_fs $remote_fs | |
# Required-Stop: $network $local_fs $remote_fs | |
# Should-Start: $named | |
# Should-Stop: | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: rails server | |
# Description: rails server | |
# | |
### END INIT INFO | |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
DAEMONUSER=${DAEMONUSER:-master} | |
. /lib/lsb/init-functions | |
set -e | |
sig () { | |
test -s "$PIDFILE" && kill -$1 `cat "$PIDFILE"` | |
} | |
oldsig () { | |
test -s "$OLD_PIDFILE" && kill -$1 `cat "$OLD_PIDFILE"` | |
} | |
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|reload) | |
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PIDFILE` && exit 0 | |
echo >&2 "Couldn't reload, starting '$CMD' instead" | |
$CMD | |
;; | |
upgrade) | |
sig USR2 && echo Upgraded && exit 0 | |
echo >&2 "Couldn't upgrade, 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|upgrade|rotate|force-stop>" | |
exit 1 | |
;; | |
esac | |
} | |
setup () { | |
export PIDFILE=$APP_ROOT/tmp/unicorn.pid | |
export OLD_PIDFILE="$PIDFILE.oldbin" | |
echo -n "$APP_ROOT: " | |
cd $APP_ROOT || exit 1 | |
CMD="start-stop-daemon --start -d $APP_ROOT --pidfile $PIDFILE --chuid $DAEMONUSER \ | |
--exec $BUNDLE exec unicorn -- -c config/unicorn.rb -E $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 | |
. $2 | |
setup | |
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