Skip to content

Instantly share code, notes, and snippets.

@dongalor
Forked from adamhunter/unicorn.sh
Created September 8, 2011 00:31
Show Gist options
  • Save dongalor/1202278 to your computer and use it in GitHub Desktop.
Save dongalor/1202278 to your computer and use it in GitHub Desktop.
rc.d script for freebsd, multiply instances of unicorn made easy...
#!/bin/sh
# Modified by [email protected]
#
# A sample /etc/unicorn/my_app.conf
# RAILS_ENV=production
# RAILS_ROOT=/var/apps/www/my_app/current
. /etc/rc.subr
USER="your_user_name"
TERM="xterm-color"
export TERM
name="unicorn"
rcvar=`set_rcvar`
[ -z "$unicorn_enable" ] && unicorn_enable="NO"
load_rc_config $name
sig () {
test -s "$PID"
}
check_process () {
LOG_DATE=`date +'%d-%m-%y %H:%M'`
export LOG_DATE
export FILE="[ "`echo $1 | cut -d '/' -f5`" ]"
echo "$LOG_DATE $FILE checking if unicorn.pid is present"
if [ -f $1 ]; then
export PID=`cat $1`
echo "$LOG_DATE $FILE is here and now let's check the process $PID"
kill -0 $PID
[ $? = 0 ] && kill -TERM $PID && echo "$FILE executed kill command for $PID process"
rm $1; echo "$FILE Removed dead pid"
else
echo "$LOG_DATE $FILE pid file not found and it was a $2"
fi
}
cmd () {
case $1 in
start)
check_process $PIDFILE "start"
su - $USER -c "$CMD"
echo "$FILE Started clean..."
;;
stop)
check_process $PIDFILE "stop"
echo "$FILE Stopped succesful"
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
exit 1
;;
esac
}
setup () {
PIDFILE="$RAILS_ROOT/tmp/pids/unicorn.pid"
export PIDFILE
[ -f $RAILS_ROOT/config.ru ] && CMD="cd $RAILS_ROOT && ./bin/unicorn -c $RAILS_ROOT/config/unicorn.rb -E $RAILS_ENV -D &> /dev/null" || CMD="cd $RAILS_ROOT && ./bin/unicorn_rails -c $RAILS_ROOT/config/unicorn.rb -E $RAILS_ENV -D &> /dev/null"
}
start_stop () {
# either run the start/stop/reload/etc command for every config under /etc/unicorn
# or just do it for a specific one
# $1 contains the start/stop/etc command
for CONFIG in /etc/unicorn/*.conf; do
# import the variables
. $CONFIG
LOG_DATE=`date +'%d-%m-%y %H:%M'`
export LOG_DATE
FILE="[ "`echo $RAILS_ROOT | cut -d '/' -f5`" ]"
export FILE
echo "$LOG_DATE $FILE is initializing..."
setup
cmd $1
done
}
ARGS="$1"
start_stop $ARGS
@dongalor
Copy link
Author

Please comment, update, I'm ready to make it much more beautiful, cuz now it still looks made hurriedly, by the way I'm just trying to understand if it's ok to write rc.d scripts like this, because my favorite linux distro Gentoo uses a lot of helpers for init.d scripts.

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