Skip to content

Instantly share code, notes, and snippets.

@jesperronn
Forked from matiaskorhonen/gist:420880
Created June 14, 2012 20:17
Show Gist options
  • Save jesperronn/2932687 to your computer and use it in GitHub Desktop.
Save jesperronn/2932687 to your computer and use it in GitHub Desktop.
Unicorn /etc/init.d/unicorn script
#!/bin/bash
### BEGIN INIT INFO
# Provides: Unicorn
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 4 6
# Short-Description: Start unicorn web server at boot - port 3000
# Description: Enable cancerventetid application at boot time. Start the unicorn web server (port 3000)
### END INIT INFO
#
#
# Add the script to /etc/init.d/APP_NAME and run the following commands to get it to run on boot:
#
# sudo chmod +x /etc/init.d/APP_NAME
# sudo update-rc.d APP_NAME defaults
#
# And that should do it?~@?
#
# 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:
APP_NAME=cancerventetid
APP_ROOT="/srv/www/$APP_NAME/current"
#PID="/srv/www/$APP_NAME/shared/pids/unicorn.pid"
PID="$APP_ROOT/tmp/pids/unicorn.pid"
USER=cancerventetid
ENV=production
GEM_HOME="/srv/www/cancerventetid/shared/bundle/ruby/1.9.1/"
UNICORN_OPTS="-D -E $ENV -c $APP_ROOT/config/unicorn/production.rb"
SET_PATH="cd $APP_ROOT; export GEM_HOME=$GEM_HOME"
CMD="$SET_PATH; $GEM_HOME/bin/unicorn $UNICORN_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 -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