Skip to content

Instantly share code, notes, and snippets.

@Jpuelpan
Forked from rodw/pid-file-daemon.sh
Last active September 3, 2015 18:50
Show Gist options
  • Save Jpuelpan/8eb7657ab04fa275ee45 to your computer and use it in GitHub Desktop.
Save Jpuelpan/8eb7657ab04fa275ee45 to your computer and use it in GitHub Desktop.
Unicorn Basic configuration
APP_ROOT = File.expand_path(File.dirname(File.dirname(__FILE__)))
worker_processes 1
working_directory APP_ROOT
timeout 30
listen APP_ROOT + "/tmp/appname.sock", backlog: 64
listen "127.0.0.1:8080", tcp_nopush: true
pid APP_ROOT + "/tmp/appname.pid"
stderr_path APP_ROOT + "/log/appname.stderr.log"
stdout_path APP_ROOT + "/log/appname.stdout.log"
#!/bin/bash
# Bash script for daemonizing an application powered by Unicorn
usage() {
echo "Usage: `basename $0` {start|stop|restart|force-stop|force-restart|status}" >&2
}
PROGRAM_NAME="My Application"
PIDFILE="tmp/pidfile.pid"
UNICORN_CONFIG="config/unicorn.rb"
APP_ENV="production"
#Get the PID from PIDFILE if we don't have one yet.
if [[ -e "$(pwd)/${PIDFILE}" ]]; then
PID=$(cat ${PIDFILE})
fi
start() {
if [[ -n $1 ]]; then
APP_ENV=$1
fi
if [[ -n "${PID}" && -n $(ps -p ${PID} | tail -n +2) ]]; then
echo "$PROGRAM_NAME is already running (PID:${PID})"
else
echo "Starting $PROGRAM_NAME with Unicorn config at $(pwd)/${UNICORN_CONFIG} in ${APP_ENV} environment"
RAILS_ENV=$APP_ENV bundle exec rake db:migrate # Run pending migrations
RAILS_ENV=$APP_ENV bundle exec rake assets:precompile # Precompile assets
bundle exec unicorn -D -c "$(pwd)/${UNICORN_CONFIG}" -E $APP_ENV
fi
}
status() {
if [[ -z "${PID}" ]]; then
echo "${PROGRAM_NAME} is not running (missing PID)."
elif [[ -n $(ps -p ${PID} | tail -n +2) ]]; then
echo "${PROGRAM_NAME} is running (PID: ${PID})."
else
echo "${PROGRAM_NAME} is not running (tested PID: ${PID})."
fi
}
stop() {
if [[ -z "${PID}" ]]; then
echo "${PROGRAM_NAME} is not running (missing PID)."
elif [[ -z $(ps -p ${PID} | tail -n +2) ]]; then
echo "${PROGRAM_NAME} is not running (tested PID: ${PID})."
else
echo "Stopping ${PROGRAM_NAME} in PID: ${PID}"
kill $1 ${PID}
fi
}
case "$1" in
start)
start $2;
;;
restart)
stop; sleep 1; start $2;
;;
stop)
stop
;;
force-stop)
stop -9
;;
force-restart)
stop -9; sleep 1; start $2;
;;
status)
status
;;
*)
usage
exit 4
;;
esac
exit 0
######################################################################
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law.
#
# If your jurisdiction supports the concept of Public Domain works,
# this program is released into the Public Domain.
#
# Otherwise this program is available under the following terms:
#---------------------------------------------------------------------
# Copyright (c) 2012, Rodney Waldhoff
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this program with or without this notice.
######################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment