总算找到一个好用的脚本, 能将多个 unicorn 实例集成到 Linux 的系统服务中, 这样就不必担心重新启动后 unicorn 是否运行了.
/etc/init.d/unicorn:
#!/bin/sh
#
# init.d script for single or multiple unicorn installations. Expects at least one .conf
# file in /etc/unicorn
#
# Modified by jay@gooby.org http://github.com/jaygooby
# 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
#
# 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: mysql networking
# Required-Stop: mysql networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This daemon is for unicorns
# Description: Starts or stops all unicorns in /etc/unicorn/*.conf
### END INIT INFO
AS_USER=deploy
set -e
sig () {
test -s "$PID" && kill -$1 `cat "$PID"`
}
oldsig () {
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"`
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
cmd () {
case $1 in
start)
sig 0 && echo >&2 "Already running" && continue
echo "Starting"
run "$CMD"
;;
stop)
sig QUIT && echo "Stopping" && continue
echo >&2 "Not running"
;;
force-stop)
sig TERM && echo "Forcing a stop" && continue
echo >&2 "Not running"
;;
restart|reload)
sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && continue
echo >&2 "Couldn't reload, starting '$CMD' instead"
run $CMD
;;
upgrade)
sig USR2 && echo Upgraded && continue
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run $CMD
;;
rotate)
sig USR1 && echo rotated logs OK && continue
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 "
exit 1
;;
esac
}
setup () {
echo -n "$RAILS_ROOT: "
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID="$PID.oldbin"
CMD="cd $RAILS_ROOT ; bundle exec unicorn -c config/unicorn.rb -E $RAILS_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
😀


One response to “笔记: Unicorn & LSB init.d”
Although I can’t understand this article, but also to see again. Do you know?