Skip to content

Instantly share code, notes, and snippets.

@alkalinecoffee
Last active December 21, 2015 00:39
Show Gist options
  • Save alkalinecoffee/6221976 to your computer and use it in GitHub Desktop.
Save alkalinecoffee/6221976 to your computer and use it in GitHub Desktop.
An init.d startup script for a Rails app running Unicorn. This setup uses a system-wide Ruby installation and deployments are handled by Capistrano.
#!/bin/bash
# chkconfig: 2345 90 10
# description: init.d script for starting a Rails app running Unicorn.
set -e
USER=rails_deployer
APP_NAME=rails_test
APP_ROOT=/var/www/$APP_NAME
CURRENT_PATH=$APP_ROOT/current
CONFIG_PATH=$CURRENT_PATH/config/unicorn.rb
CMD="cd $CURRENT_PATH; bundle exec unicorn -c $CONFIG_PATH -E production -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
cd $CURRENT_PATH || exit 1
sig () {
test -s "$PID" && kill -$1 `cat "$PID"`
}
oldsig () {
test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"`
}
case ${1-help} in
start)
sig 0 && echo >&2 "Already running" && exit 0
su - $USER -c "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
su - $USER -c "$CMD"
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - $USER -c "$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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment