Skip to content

Instantly share code, notes, and snippets.

@bartimaeus
Created June 30, 2012 03:58
Show Gist options
  • Save bartimaeus/3022177 to your computer and use it in GitHub Desktop.
Save bartimaeus/3022177 to your computer and use it in GitHub Desktop.
Init script for rails application, originally developed by mikesmullin
#!/bin/bash
### BEGIN INIT INFO
# Provides: APPLICATION
# Required-Start: $all
# Required-Stop: $network $local_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the APPLICATION unicorns at boot
# Description: Enable APPLICATION at boot time.
### END INIT INFO
#
# Use this as a basis for your own Unicorn init script.
# Change APPLICATION to match your app.
# Make sure that all paths are correct.
set -u
set -e
# Change these to match your app:
USER_NAME=[rvm_user]
APP_NAME=[app_name]
APP_ROOT="/var/www/$APP_NAME/current"
PID="/var/www/$APP_NAME/current/tmp/pids/unicorn.pid"
ENV=production
GEM_HOME="/home/$USER_NAME/.rvm/gems/ruby-1.9.3-p194"
APP_OPTS="-D -E $ENV -c $APP_ROOT/config/unicorn.rb"
APP_PATH="cd $APP_ROOT; rvm use 1.9.3; export GEM_HOME=$GEM_HOME"
CMD="$APP_PATH; bundle exec $GEM_HOME/bin/unicorn_rails $APP_OPTS"
old_pid="$PID.oldbin"
cd $APP_ROOT || 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_NAME -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_NAME -c "$CMD"
;;
upgrade)
sig USR2 && exit 0
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - $USER_NAME -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
@bartimaeus
Copy link
Author

  • We'll create this script as /etc/init.d/[app_name]
  • Next, make the script executable chmod +x /etc/init.d/[app_name]
  • Then, we'll make it load when the server restarts: cd /etc/init.d && update-rc.d [app_name] defaults

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment