Skip to content

Instantly share code, notes, and snippets.

@niwo
Forked from mguterl/gist:308216
Created November 22, 2011 10:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niwo/1385341 to your computer and use it in GitHub Desktop.
Save niwo/1385341 to your computer and use it in GitHub Desktop.
Unicorn init.d script (chkconfig compatible) /etc/init.d/unicorn with example application configuration scripts
# starts all unicorns listed in /etc/unicorn/*.conf
# lives under /etc/unicorn/rpcm.conf
ENV=production
APP_ROOT=/opt/rpcm
# Example Unicorn configuration for sinatra app
# must live under <APP_ROOT>/config/unicorn.rb
APP_ROOT = File.expand_path '../', File.dirname(__FILE__)
worker_processes 2
working_directory APP_ROOT
pid "#{APP_ROOT}/tmp/unicorn.pid"
# This is where we specify the socket.
# We will point the upstream nginx module to this socket later on
listen "#{APP_ROOT}/tmp/sockets/unicorn.sock", :backlog => 64
# alternatively use "listen 8080" to listen on a tcp port
stderr_path "#{APP_ROOT}/log/unicorn.stderr.log"
stdout_path "#{APP_ROOT}/log/unicorn.stdout.log"
before_fork do |server, worker|
old_pid = "#{APP_ROOT}/tmp/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
#!/bin/sh
#
# 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
#
# Modified by wol
# based on http://gist.github.com/308216 by http://github.com/mguterl
#
## 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
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" && 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_PID` && 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 () {
echo -n "$APP_ROOT: "
cd $APP_ROOT || exit 1
export PID=$APP_ROOT/tmp/unicorn.pid
export OLD_PID="$PID.oldbin"
CMD="/usr/bin/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
@Aslan
Copy link

Aslan commented Jan 18, 2012

hi niwo... i have a copy of your unicorn init.d script. my problem was that most of the time restarts failed. i started playing around with the sleep time and reducing it to sleep 2 fixed the issue. Any thought on that?

@niwo
Copy link
Author

niwo commented Jan 18, 2012

hi aslan!
My gist is based on the script found here: https://gist.github.com/504875.
Seems strange to me that 5 seconds of sleep would break the script, never experienced such behavior.

@Aslan
Copy link

Aslan commented Jan 19, 2012

Hi niwo

I think the issue was indirectly related. I was trying to kill the old pid both in the init.d script and unicorn.rb. Most of the time unicorn.rb's before fork block did remove the old pid before the init.d and that would cause init.d to not exit properly. reducing the sleep to 2 seconds sometimes gave the init.d the time it required to kill the old pid before unicorn.rb did. Thats why i thought i was fixing the problem. I am not killing the old pid in the init.d any more and every thing seems to be working fine now. Any idea way a 5 second sleep was placed in the first place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment