Skip to content

Instantly share code, notes, and snippets.

@rockstar2046
Last active August 29, 2015 14:06
Show Gist options
  • Save rockstar2046/275d8d635f82f79b6e25 to your computer and use it in GitHub Desktop.
Save rockstar2046/275d8d635f82f79b6e25 to your computer and use it in GitHub Desktop.
unicorn service script
#! /usr/bin/env bash
#
# Control unicorn server
#
# NOTICE:
# Environment -> production
#
# Author: Tony
#
# Model
RAILS_ENV=production
export RAILS_ENV
# Relative url root
# deploying to a subdirectory
#export RAILS_RELATIVE_URL_ROOT=/app
# Secret key
SECRET_KEY_BASE=72fbdb1f8562281cef4c7095374d6ba5dbbf1271
export SECRET_KEY_BASE
# Unicorn
# work_path
WORK_PATH=${TMPDIR:-/tmp/${PWD##*/}}
export WORK_PATH
mkdir -p "$WORK_PATH/log"
UNICORN_PID="${WORK_PATH}/unicorn.pid"
ACTION=$1
running(){
if [ -f "$1" ]
then
local PID=$(cat "$1" 2>/dev/null) || return 1
kill -0 "$PID" 2>/dev/null
return
fi
rm -f "$1"
return 1
}
stop(){
if running $UNICORN_PID
then
kill $(cat "$UNICORN_PID")
sleep 1
fi
rm -f "$UNICORN_PID"
echo OK
}
start(){
if running $UNICORN_PID
then
echo "Already Running $(cat $UNICORN_PID)!"
exit 1
fi
# assets
bundle exec rake assets:precompile
unicorn_rails -D -c config/unicorn.rb
if [[ "$?" -eq 0 ]]
then
echo OK.
else
echo FAILED.
fi
}
################################
# Do the action
################################
case "$ACTION" in
start)
echo "Start unicorn server..."
start
;;
stop)
echo "Stop unicorn server..."
stop
;;
restart)
"$0" stop
"$0" start
;;
*)
echo "Usage: ${0} {start|stop|restart}"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment