Skip to content

Instantly share code, notes, and snippets.

@troex
Created April 4, 2011 20:57
Show Gist options
  • Save troex/902420 to your computer and use it in GitHub Desktop.
Save troex/902420 to your computer and use it in GitHub Desktop.
Unicorn start/stop/restart script for Debian
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn-kk
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: mysql memcached sphinx-kk
# Should-Stop: mysql memcached sphinx-kk
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: unicorn kk
# Description: unicorn kk
### END INIT INFO
set -u
set -e
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals
# Feel free to change any of the following variables for your app:
APP_ROOT=/var/www/app/current
PID=/var/www/app/shared/pids/unicorn.pid
ENV=production
CMD="/usr/bin/unicorn_rails -D -E $ENV -c config/unicorn.rb"
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 in
start)
sig 0 && echo >&2 "Already running" && exit 0
$CMD
echo >&2 "Started"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload"
#$CMD
;;
restart)
sig USR2
echo >&2 "Starting new master, waiting 10 sec..."
sleep 10
echo -n >&2 "Quiting old master... "
oldsig QUIT
echo >&2 "done"
#echo >&2 "Couldn't upgrade, starting '$CMD' instead"
#$CMD
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 "Usage: $0 <start|stop|reload|restart|rotate|force-stop>"
exit 1
;;
esac
@troex
Copy link
Author

troex commented May 21, 2012

TODO

  • restart sleep time
  • workers in config
  • custom unicorn config
  • option to disable current unicorn
  • unset variables before run
  • better output
  • docs / examples
  • move to github

@patrick-higgins
Copy link

This works great! I needed to add the -H option to sudo to be able to use rvm, though. Here's my /etc/unicorn/wsoacert-ci.conf:

RAILS_ENV=ci
RAILS_ROOT=/srv/www/wsoacert/current
USER=wsoaref
START_CMD="/home/wsoaref/.rvm/bin/rvm in $RAILS_ROOT do bundle exec unicorn"
UNICORN_CONFIG=$RAILS_ROOT/config/unicorn.rb

Without -H, the HOME environment variable is not set and RVM complains with Can't find rvm install!

@knyar
Copy link

knyar commented Mar 3, 2013

Here's a script that supports RVM properly: https://github.com/knyar/unicorn-rvm-init

@drasill
Copy link

drasill commented Mar 3, 2013

Thanks, this looks great.

@inspire22
Copy link

@patrick-higgins - your code pasted in doesn't contain any "-H"...?

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