Skip to content

Instantly share code, notes, and snippets.

@nicobrevin
Created July 1, 2010 17:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicobrevin/460263 to your computer and use it in GitHub Desktop.
Save nicobrevin/460263 to your computer and use it in GitHub Desktop.
jsvc-daemon
#! /bin/sh
# Generic script for running ruby scripts as daemons using
# jsvc and a java class to control the daemon.
#
# Contains common parameters and start/stop
# Things you'll need to set on a per script/daemon basis:
# SCRIPT_NAME
#
# Things you can set:
# PROG_OPTS - Arguments to send to the program. A few defaults are appended to this.
# Local development - uncomment these to use jsvc-daemon from a working copy
#JSVC=`which jsvc`
#JAVA_HOME=`jruby -e 'puts Java::JavaLang::System.get_property("java.home")'`
#JRUBY_HOME=`jruby -e 'puts Java::JavaLang::System.get_property("jruby.home")'`
#APP_HOME=.
#PIDFILE=jsvc-$SCRIPT_NAME.pid
#LOG_DIR=log
# Standard install
JSVC=/usr/bin/jsvc
JAVA_HOME=/usr/lib/jvm/java-6-sun
JRUBY_HOME=/usr/lib/jruby1.4
APP_HOME=/usr/lib/yourapp
USER=jruby
PIDFILE=/var/run/msp/jsvc-$SCRIPT_NAME.pid
LOG_DIR=/var/log
# If you want your programs to run as or not as daemons pass a flag to tell them which they are
PROG_OPTS+=" --no-log-stdout --daemon"
# Implement the jsvc Daemon interface.
MAIN_CLASS=com.msp.JRubyDaemon
# Uncomment for debugging the daemon script
# JSVC_ARGS_EXTRA="-debug -nodetach"
RUBY_SCRIPT=$APP_HOME/bin/$SCRIPT_NAME
CLASSPATH=$JRUBY_HOME/lib/jruby.jar:$JRUBY_HOME/lib/profile.jar:/usr/share/java/commons-daemon.jar:$APP_HOME/vendor/jars/yourapp-java.jar # make sure your implementation of Daemon is on the classpath
JAVA_PROPS="-Djruby.memory.max=500m -Djruby.stack.max=1024k -Djna.boot.library.path=$JRUBY_HOME/lib/native/linux-i386:$JRUBY_HOME/lib/native/linux-amd64 -Djffi.boot.library.path=$JRUBY_HOME/lib/native/i386-Linux:$JRUBY_HOME/jruby/lib/native/s390x-Linux:$JRUBY_HOME/lib/native/x86_64-Linux -Djruby.home=$JRUBY_HOME -Djruby.lib=$JRUBY_HOME/lib -Djruby.script=jruby -Djruby.shell=/bin/sh"
JAVA_OPTS="-Xmx500m -Xss1024k -Xbootclasspath/a:$JRUBY_HOME/lib/jruby.jar:$JRUBY_HOME/lib/bsf.jar"
JSVC_ARGS="-home $JAVA_HOME \
$JSVC_ARGS_EXTRA\
-wait 20\
-pidfile $PIDFILE\
-user $USER \
-procname jsvc-$SCRIPT_NAME\
-jvm server\
-outfile $LOG_DIR/jsvc-$SCRIPT_NAME.log\
-errfile &1"
#
# Stop/Start
#
case "$1" in
start)
if [ -e "$PIDFILE" ]; then
echo "Pidfile exists, not starting."
else
echo "Starting $SCRIPT_NAME daemon..."
$JSVC $JSVC_ARGS -cp $CLASSPATH $JAVA_PROPS $JAVA_OPTS $MAIN_CLASS $RUBY_SCRIPT $PROG_OPTS
EXIT_CODE=$?
if [ "$EXIT_CODE" != 0 ]; then
echo "Daemon exited with status: $EXIT_CODE. Check pidfile and log..."
fi
fi
;;
stop)
if [ -e "$PIDFILE" ]; then
$JSVC $JSVC_ARGS -stop $MAIN_CLASS
else
echo "No pid file, not stopping."
fi
;;
*)
echo "Unrecognised command. Usage jsvc-daemon [ start | stop ]"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment