Skip to content

Instantly share code, notes, and snippets.

@davidlfox
Created March 14, 2014 03:57
Show Gist options
  • Save davidlfox/9541911 to your computer and use it in GitHub Desktop.
Save davidlfox/9541911 to your computer and use it in GitHub Desktop.
Unicorn setup
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: unicorn initscript
# Description: unicorn
### END INIT INFO
set -e
NAME=unicorn
DESC="Unicorn web server"
. /lib/lsb/init-functions
if [ -f /etc/default/unicorn ]; then
. /etc/default/unicorn
fi
export GEM_HOME=/usr/local/rvm/gems/ruby-2.0.0-p353
export GEM_PATH=/usr/local/rvm/gems/ruby-2.0.0-p353:/usr/local/rvm/gems/ruby-2.0.0-p353/gems:/usr/local/rvm/gems/ruby-2.0.0-p353@global/gems
DAEMON=/usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn
PID=${PID-/run/unicorn.pid}
run_by_init() {
([ "${previous-}" ] && [ "${runlevel-}" ]) || [ "${runlevel-}" = S ]
}
exit_with_message() {
if ! run_by_init; then
log_action_msg "$1 Not starting."
fi
exit 0
}
check_config() {
if [ $CONFIGURED != "yes" ]; then
exit_with_message "Unicorn is not configured (see /etc/default/unicorn)."
fi
}
check_app_root() {
if ! [ -d $APP_ROOT ]; then
exit_with_message "Application directory $APP_ROOT is not exist."
fi
}
set -u
case "$1" in
start)
check_config
check_app_root
log_daemon_msg "Starting $DESC" $NAME || true
if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
stop)
log_daemon_msg "Stopping $DESC" $NAME || true
if start-stop-daemon --stop --signal QUIT --quiet --oknodo --pidfile $PID; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
force-stop)
log_daemon_msg "Forcing stop of $DESC" $NAME || true
if start-stop-daemon --stop --quiet --oknodo --pidfile $PID; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" $NAME || true
start-stop-daemon --stop --quiet --oknodo --pidfile $PID
sleep 1
if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
reload)
log_daemon_msg "Reloading $DESC" $NAME || true
if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PID; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
reopen-logs)
log_daemon_msg "Relopening log files of $DESC" $NAME || true
if start-stop-daemon --stop --signal USR1 --quiet --oknodo --pidfile $PID; then
log_end_msg 0 || true
else
log_end_msg 1 || true
fi
;;
status)
status_of_proc -p $PID $DAEMON $NAME && exit 0 || exit $?
;;
*)
log_action_msg "Usage: $0 <start|stop|restart|force-reload|reload|force-stop|reopen-logs|status>" || true
exit 1
;;
esac
# This is DigitalOcean's stock unicorn file when creating
# a new Ubuntu 12.10 droplet with nginx/rails/unicorn
# the app in /home/rails loads fine with the vanilla install
listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "/home/rails"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"
working_directory "/var/www/myapp/current"
worker_processes 2 # this should be >= nr_cpus
pid "/home/myuser/pids/unicorn.pid"
stderr_path "/home/myuser/log/unicorn.log"
stdout_path "/home/myuser/log/unicorn.log"
@davidlfox
Copy link
Author

output from running unicorn -c /var/www/myapp/config/unicorn.rb -D -d

{:unicorn_options=>
  {:listeners=>[],
   :config_file=>"/var/www/myapp/current/config/unicorn.rb"},
 :app=>
  #<Proc:0x00000001b98a38@/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:43 (lambda)>,
 :daemonize=>true}

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