Skip to content

Instantly share code, notes, and snippets.

@glebtv
Created August 5, 2013 04:13
Show Gist options
  • Save glebtv/6153461 to your computer and use it in GitHub Desktop.
Save glebtv/6153461 to your computer and use it in GitHub Desktop.
app init script
#!/bin/sh
#
# init.d script for single unicorn installation.
#
# This configures a unicorn master for your app at RAILS_ROOT 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.
#
RUBY_VER=2.0.0-p247
APP=ml
APP_ROOT=/data/$APP/app
APP_ENV=production
APP_USER=$APP
HOME="/data/ml"
PID_FILE=$APP_ROOT/shared/pids/unicorn.pid
OLD_PID_FILE="$PID_FILE.oldbin"
CMD="[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"; cd $APP_ROOT/current; rvm use ruby-$RUBY_VER@$APP; bundle exec unicorn -c $APP_ROOT/current/config/unicorn.rb -E $APP_ENV -D"
set -e
sig () {
test -s "$PID_FILE" && kill -$1 `cat "$PID_FILE"`
}
oldsig () {
test -s "$OLD_PID_FILE" && kill -$1 `cat "$OLD_PID_FILE"`
}
cmd () {
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting"
su - $APP_USER -c "$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_FILE` && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
su - $APP_USER -c "$CMD"
;;
reexec)
sig USR2 && echo "Upgraded" && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - $APP_USER -c "$CMD"
;;
stop-old)
oldsig QUIT && echo "Old master stopped" && exit 0
echo >&2 "Couldn't stop old master"
;;
rotate)
sig USR1 && echo "rotated logs OK" && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
add-worker)
sig TTIN && echo "added one worker" && exit 0
echo >&2 "Couldn't add worker" && exit 1
;;
del-worker)
sig TTOU && echo "removed one worker" && exit 0
echo >&2 "Couldn't remove worker" && exit 1
;;
status)
if [ -s $PID_FILE ]; then
ppid=`cat $PID_FILE`
kill -0 $ppid >/dev/null 2>&1
if [ "$?" = "0" ]; then
echo "$APP is running (pid $ppid)"
else
echo "$APP is stopped (pidfile is stale)"
fi
else
echo "$APP is stopped (no pidfile)"
fi
;;
*)
echo >&2 "Usage: $0 <status|start|stop|restart|reexec|stop-old|rotate|add-worker|del-worker|force-stop>"
exit 1
;;
esac
}
echo -n "$APP: "
cmd $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment