Skip to content

Instantly share code, notes, and snippets.

@madrobby madrobby/unicorn
Created Mar 26, 2014

Embed
What would you like to do?
Unicorn init.d script (for Ubuntu 12.04 LTS)
#! /bin/sh
# File: /etc/init.d/unicorn
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn web server
# Description: starts unicorn
### END INIT INFO
TIMEOUT=${TIMEOUT-60}
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 "Force stopping" && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo "Reloading" && exit 0
echo >&2 "Couldn't reload, starting instead"
$CMD
;;
upgrade)
printf 'Upgrading...'
if sig USR2 && sleep 10 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PID && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
if test $n -lt 0 && test -s $OLD_PID
then
echo
echo >&2 "$OLD_PID still exists after $TIMEOUT seconds"
exit 1
fi
echo "...complete."
exit 0
fi
echo >&2 "Couldn't upgrade, starting instead"
$CMD
;;
reopen-logs)
sig USR1
echo "Reopened logs"
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
}
setup () {
PID=/u/apps/freckle/current/tmp/pids/unicorn.pid
OLD_PID="$PID.oldbin"
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export BUNDLE_GEMFILE=/u/apps/freckle/current/Gemfile
export RUBY_HEAP_MIN_SLOTS=750000
export RUBY_HEAP_SLOTS_INCREMENT=250000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=100000000
CMD="/usr/local/bin/bundle exec unicorn_rails -c /etc/freckle_production.rb -E production -D"
}
start_stop () {
setup
cmd $1
}
ARGS="$1"
start_stop $ARGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.