Skip to content

Instantly share code, notes, and snippets.

@thelowlypeon
Last active March 26, 2017 00:38
Show Gist options
  • Save thelowlypeon/cd991b0e7848419aa42de51b942b6ded to your computer and use it in GitHub Desktop.
Save thelowlypeon/cd991b0e7848419aa42de51b942b6ded to your computer and use it in GitHub Desktop.
Scripts to starting, stopping, restarting, or rotating a unicorn server
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn app server
# Description: starts unicorn using start-stop-daemon
### END INIT INFO
# this should go into /etc/init.d/unicorn_server
set -e
USAGE="Usage: $0 <start|stop|restart|rotate|force-stop|status>"
USER="deploy"
# make sure the app exists
cd $BIN_DIR || exit 1
cmd() {
su - $USER -c "/bin/bash /home/$USER/unicorn_server $1"
}
check_status () {
if [ -f $PID ]; then
echo "pid exists, service is running"
return 0
else
echo "pid doesn't exist, service is not running"
return 4
fi
}
case "$1" in
start)
cmd "start"
;;
stop)
cmd "stop"
;;
force-stop)
cmd "kill"
;;
restart|reload|upgrade)
cmd "restart"
;;
rotate)
cmd "rotate"
;;
status)
check_status
exit $?
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
#!/bin/sh
if [ `whoami` != "deploy" ]; then
echo "Must run script as deploy"
exit 1
fi
USAGE="/bin/bash unicorn_server (start|stop|restart|kill|rotate|status)"
APP_ROOT="/var/www/current"
SHARED_DIR="/var/www/shared"
GEM_PATH="$SHARED_DIR/bundle"
ENV="production"
PORT=8080
PID="$SHARED_DIR/tmp/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
case "$1" in
start)
if [ -f $PID ]; then
echo "Unicorn is already running with PID `cat $PID`"
else
echo "Starting unicorn"
GEM_PATH=$GEM_PATH cd $APP_ROOT && bundle exec unicorn -c $SHARED_DIR/unicorn.rb -E $ENV -p $PORT -D
fi
;;
stop)
if [ -f $PID ]; then
echo "Stopping Unicorn process `cat $PID`"
kill -QUIT `cat $PID`
else
echo "Unicorn is not running"
fi
;;
kill)
if [ -f $PID ]; then
echo "Force-killing Unicorn process `cat $PID`"
kill -TERM `cat $PID`
else
echo "Unicorn is not running"
fi
;;
restart|reload|upgrade)
if [ -f $PID ]; then
echo "Restarting Unicorn `cat $PID`"
kill -USR2 `cat $PID`
sleep 5
echo "Killing old Unicorn process `cat $OLD_PID`"
kill -QUIT `cat $OLD_PID`
else
echo "Unicorn is not running, starting manually"
/bin/bash ../start.sh
fi
;;
rotate)
if [ -f $PID ]; then
echo "Rotating logs for Unicorn process `cat $PID`"
kill -USR1 `cat $PID`
else
echo "Unicorn is not running"
fi
;;
status)
if [ -f $PID ]; then
echo "pid exists, service is running"
exit 0
else
echo "pid doesn't exist, service is not running"
exit 4
fi
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
@thelowlypeon
Copy link
Author

make this into an ubuntu service by throwing this into /etc/init.d/unicorn_server:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the unicorn app server
# Description:       starts unicorn using start-stop-daemon
### END INIT INFO

set -e

USAGE="Usage: $0 <start|stop|restart|rotate|force-stop|status>"
USER="deploy"

# make sure the app exists
cd $BIN_DIR || exit 1

cmd() {
  su - $USER -c "/bin/bash /home/$USER/unicorn_server $1"
}

check_status () {
  if [ -f $PID ]; then
    echo "pid exists, service is running"
    return 0
  else
   echo "pid doesn't exist, service is not running"
    return 4
  fi
}

case "$1" in
  start)
    cmd "start"
    ;;
  stop)
    cmd "stop"
    ;;
  force-stop)
    cmd "kill"
    ;;
  restart|reload|upgrade)
    cmd "restart"
    ;;
  rotate)
    cmd "rotate"
    ;;
  status)
    check_status
    exit $?
    ;;
  *)
    echo >&2 $USAGE
    exit 1
    ;;
esac

then run:

$ sudo update-rc.d unicorn_server defaults
$ sudo service unicorn_server start

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