Created
September 2, 2011 08:42
-
-
Save junegunn/1188197 to your computer and use it in GitHub Desktop.
init.d-style jruby daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Junegunn Choi ([email protected]) | |
# 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