Skip to content

Instantly share code, notes, and snippets.

@junegunn
Created September 2, 2011 08:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junegunn/1188197 to your computer and use it in GitHub Desktop.
Save junegunn/1188197 to your computer and use it in GitHub Desktop.
init.d-style jruby daemon
#!/bin/bash
# Junegunn Choi (junegunn.c@gmail.com)
# 2011/09/02-
# init.d-style daemon script for JRuby with spoon gem.
APP_NAME=sleeper
APP_PATH=/Users/jg/github/jruby-daemon/sleeper.rb
JRUBY=/Users/jg/.rvm/rubies/jruby-1.6.4/bin/ruby
export GEM_HOME=~/.rvm/gems/jruby-1.6.4
LOGFILE=/var/log/$APP_NAME.log
ERRFILE=/var/log/$APP_NAME.err
PIDFILE=/var/run/$APP_NAME.pid
# ---------------------------------------------------
WRAPPER='
require "rubygems";
require "spoon";
pid = Spoon.spawnp(*ARGV[1..-1]);
File.open(ARGV[0], "w") { |f| f << pid };
'
COMMAND="$JRUBY -e '$WRAPPER' '$PIDFILE' '$JRUBY' '$APP_PATH'"
start_daemon() {
if [ -e "$PIDFILE" ]; then
echo "PID file already exists."
exit 1
else
echo "Starting $APP_NAME."
eval $COMMAND >> $LOGFILE 2>> $ERRFILE
echo $?
fi
}
stop_daemon() {
if [ -e "$PIDFILE" ]; then
echo "Stopping $APP_NAME."
PID=`cat $PIDFILE`
while [ 1 ]; do
kill -TERM $PID 2> /dev/null
sleep 1
[ `ps $PID 2> /dev/null | grep $PID | wc -l` -eq 0 ] && break
done
rm $PIDFILE
else
echo "PID file not found."
exit 1
fi
}
case $1 in
start)
start_daemon
;;
stop)
stop_daemon
;;
restart)
stop_daemon
sleep 1
start_daemon
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment