Skip to content

Instantly share code, notes, and snippets.

@OpenSourceAlchemist
Last active February 5, 2016 13:36
Show Gist options
  • Select an option

  • Save OpenSourceAlchemist/6db7b396407c157ea73e to your computer and use it in GitHub Desktop.

Select an option

Save OpenSourceAlchemist/6db7b396407c157ea73e to your computer and use it in GitHub Desktop.
A sample unicorn supervisor script.
#!/bin/bash
# This is a helper script you can use to supervise unicorn, it allows you to perform a live restart
# by sending it a USR2 signal
LOCAL_WEB="http://127.0.0.1:<%= @unicorn_port %>/"
function on_exit()
{
kill $UNICORN_PID
echo "exiting"
}
function on_reload()
{
echo "Reloading unicorn"
kill -s USR2 $UNICORN_PID
sleep 20
curl $LOCAL_WEB &> /dev/null
NEW_UNICORN_PID=`ps -f --ppid $UNICORN_PID | grep unicorn | grep -v worker | awk '{ print $2 }'`
kill $UNICORN_PID
echo "Old pid is: $UNICORN_PID New pid is: $NEW_UNICORN_PID"
UNICORN_PID=$NEW_UNICORN_PID
}
export UNICORN_SUPERVISOR_PID=$$
trap on_exit EXIT
trap on_reload USR2
<% current_dir = File.join(@tree_root, 'current') %>
echo "Starting unicorn in <%= current_dir %>, RAILS_ENV=$RAILS_ENV"
. ~/.rvm/scripts/rvm
cd <%= current_dir %>
rvm use <%= @ruby_version %>@<%= @ruby_gemset %>
bundle exec unicorn_rails -c $1 &
UNICORN_PID=$!
echo "supervisor pid: $UNICORN_SUPERVISOR_PID unicorn pid: $UNICORN_PID"
while [ -e /proc/$UNICORN_PID ]
do
sleep 0.1
done
echo "Wo, Unicorn went away, dying....."
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment