Skip to content

Instantly share code, notes, and snippets.

@andoriyu
Created September 30, 2011 05:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andoriyu/1252768 to your computer and use it in GitHub Desktop.
Save andoriyu/1252768 to your computer and use it in GitHub Desktop.
rc.d script for unicorn (FreeBSD)
unicorn_profiles="dev prod"
unicorn_dev_enable="yes"
unicorn_dev_user="devuser"
unicorn_dev_listen="/tmp/unicorn.dev.socket"
unicorn_dev_dir="/usr/local/www/dev"
unicorn_dev_config="/usr/local/www/dev/config.ru"
unicorn_dev_env="development"
unicorn_dev_flags="" # any additional flags
unicorn_dev_rails="YES" # For rails
unicorn_prod_enable="yes"
unicorn_prod_user="produser"
unicorn_prod_listen="/tmp/unicorn.prod.socket"
unicorn_prod_dir="/usr/local/www/prod"
unicorn_prod_config="/usr/local/www/dev/config.ru"
unicorn_prod_env="production"
unicorn_prod_flags="" # any additional flags
#!/bin/sh
# PROVIDE: unicorn
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable unicorn:
# unicorn_enable (bool): Set to "NO" by default.
# Set it to "YES" to enable unicorn
# unicorn_profiles (str): Set to "" by default.
# Define your profiles here.
# unicorn_flags (str): Set to "" by default.
# Extra flags passed to start command.
. /etc/rc.subr
name="unicorn"
rcvar=`set_rcvar`
unicorn="/usr/local/bin/unicorn"
unicorn_r="/usr/local/bin/unicorn_rails"
command="/usr/sbin/daemon"
procname="ruby18"
pidprefix="/var/run/unicorn"
[ -z "$unicorn_enable" ] && unicorn_enable="NO"
load_rc_config $name
if [ -n "$2" ]; then
profile="$2"
if [ "x${unicorn_profiles}" != "x" ]; then
[ ! -d "$pidprefix" ] && mkdir -m 777 $pidprefix
pidfile="$pidprefix/${profile}.pid"
eval unicorn_enable="\${unicorn_${profile}_enable:-${unicorn_enable}}"
eval unicorn_flags="\${unicorn_${profile}_flags:-${unicorn_flags}}"
eval unicorn_listen="\${unicorn_${profile}_listen:-${unicorn_listen}}"
eval unicorn_chdir="\${unicorn_${profile}_dir:-${unicorn_dir}}"
eval unicorn_config="\${unicorn_${profile}_config:-${unicorn_config}}"
eval unicorn_user="\${unicorn_${profile}_user:-${unicorn_user}}"
eval unicorn_env="\${unicorn_${profile}_env:-${unicorn_env}}"
eval unicorn_rails="\${unicorn_${profile}_rails:-${unicorn_rails}}"
required_dirs="$unicorn_chdir"
[ "$unicorn_rails" = "YES" ] && unicorn_flags="-f -p $pidfile $unicorn_r -l $unicorn_listen ${unicorn_flags}"
[ "$unicorn_rails" != "YES" ] && unicorn_flags="-f -p $pidfile ${unicorn} -l $unicorn_listen ${unicorn_flags}"
[ -e "$pidfile" -a "$1" = "start" ] && rm $pidfile
[ -e "$unicorn_listen" -a "$1" = "start" ] && rm $unicorn_listen
[ -n "$unicorn_env" ] && unicorn_flags="$unicorn_flags -E $unicorn_env"
[ -e "$unicorn_config" ] && unicorn_flags="$unicorn_flags $unicorn_config"
echo $unicorn_flags
else
echo "$0: extra argument ignored"
fi
else
if [ "x${unicorn_profiles}" != "x" -a "x$1" != "x" ]; then
for profile in ${unicorn_profiles}; do
echo "===> unicorn profile: ${profile}"
$0 $1 ${profile} # ?
retcode="$?"
if [ "0${retcode}" -ne 0 ]; then
failed="${profile} (${retcode}) ${failed:-}"
else
success="${profile} ${success:-}"
fi
done
exit 0
fi
fi
extra_commands="reload"
sig_reload="USR2"
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment