Skip to content

Instantly share code, notes, and snippets.

@DaikiSuganuma
Last active August 29, 2015 13:56
Show Gist options
  • Save DaikiSuganuma/9226345 to your computer and use it in GitHub Desktop.
Save DaikiSuganuma/9226345 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Redmine Startup script for unicorn.
#
# chkconfig: - 86 15
# description: redmine on unicorn start/stop script.
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
# load rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
# move to project root directory
NAME=redmine
ENVIROMENT=production
# SCRIPT_DIR=`dirname $0`
ROOT_DIR="/opt/redmine"
PID="${ROOT_DIR}/tmp/pids/unicorn.pid"
CONF="${ROOT_DIR}/config/unicorn.rb"
PORT=5001
start()
{
if [ -e $PID ]; then
echo "$NAME already started";
exit 1;
fi
echo "start $NAME";
cd $ROOT_DIR
bundle exec unicorn_rails -c ${CONF} -E ${ENVIROMENT} -D -p ${PORT}
}
stop()
{
if [ ! -e $PID ]; then
echo "$NAME not started";
exit 1;
fi
echo "stop $NAME";
kill -QUIT `cat ${PID}`
rm -f $PID
}
force_stop()
{
if [ ! -e $PID ]; then
echo "$NAME not started";
exit 1;
fi
echo "stop $NAME";
kill -TERM `cat ${PID}`
rm -f $PID
}
reload()
{
if [ ! -e $PID ]; then
echo "$NAME not started";
start
exit 0;
fi
echo "reload $NAME";
kill -HUP `cat ${PID}`
}
restart()
{
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
force-stop)
force_stop
;;
reload)
reload
;;
restart)
restart
;;
*)
echo "Syntax Error: release [start|stop|force-stop|reload|restart]"
;;
esac
# Sample verbose configuration file for Unicorn (not Rack)
#
# This configuration file documents many features of Unicorn
# that may not be needed for some applications. See
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
# for a much simpler configuration file.
#
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
# Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 2
# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen File.expand_path("tmp/unicorn.sock", ENV['RAILS_ROOT'])
# nuke workers after 30 seconds instead of 60 seconds (the default)
# timeout 30
# feel free to point this anywhere accessible on the filesystem
# pid "/path/to/app/shared/pids/unicorn.pid"
pid File.expand_path("tmp/pids/unicorn.pid", ENV['RAILS_ROOT'])
# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
# stderr_path "/path/to/app/shared/log/unicorn.stderr.log"
stdout_path File.expand_path("log/unicorn.stdout.log", ENV['RAILS_ROOT'])
# stdout_path "/path/to/app/shared/log/unicorn.stdout.log"
stderr_path File.expand_path("log/unicorn.stderr.log", ENV['RAILS_ROOT'])
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
sleep 1
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment