Skip to content

Instantly share code, notes, and snippets.

@AlexRogalskiy
Created February 24, 2022 23:18
Show Gist options
  • Save AlexRogalskiy/1205bd55f8cefc002e63ef3954ab1f88 to your computer and use it in GitHub Desktop.
Save AlexRogalskiy/1205bd55f8cefc002e63ef3954ab1f88 to your computer and use it in GitHub Desktop.
init-like nailgun server runner
#!/bin/sh -e
DESCRIPTION="JRuby Nailgun Server"
ARGS="--ng-server"
# ARGS="$ARGS -Xcompat.version=1.8"
ARGS="$ARGS -Xcompat.version=1.9"
ARGS="$ARGS -Xmn512m -Xms2048m -Xmx2048m" # heap and stack
ARGS="$ARGS -J-Djruby.thread.pooling=true" # Enable thread pooling
ARGS="$ARGS -J-Djruby.objectspace.enabled=false" # Disable ObjectSpace
## Running without nailgun
#ARGS=""
##ARGS="$ARGS --server" # server runtime
##ARGS="$ARGS --client" # client runtime
##ARGS="$ARGS --1.8" # Ruby 1.8
#ARGS="$ARGS --1.9" # Ruby 1.9
##ARGS="$ARGS -J-Xmn512m -J-Xms2048m -J-Xmx2048m" # heap and stack
##ARGS="$ARGS -J-Djruby.thread.pooling=true" # Enable thread pooling
##ARGS="$ARGS -O" # Disable ObjectSpace
get_pid() {
ps -ewwopid,command | sed '/grep/d;/sed/d;/NGServ1er/!d;s/^\([0-9]*\).*/\1/g' | grep '[0-9]'
}
started() {
get_pid >/dev/null
}
complain_if() {
if $1; then
echo "$DESCRIPTION $2." >&2
false
fi
}
complain_unless() {
complain_unless "! $1" $2
}
start() {
complain_if started 'is already running'
nohup \jruby $ARGS 2>&1 | tee -a /var/log/ngserver.log >/dev/null 2>&1 &
sleep 1
complain_unless started 'failed to start'
}
stop() {
complain_unless started 'is already stopped'
kill `get_pid`
sleep 1
if started; then
kill -9 `get_pid`
sleep 1
complain_if started 'failed to stop'
fi
}
status() {
if started; then
echo "$DESCRIPTION is running" >&2
else
echo "$DESCRIPTION is stopped" >&2
false
fi
}
restart() {
stop && start
}
condrestart() {
started && restart
}
usage() {
cat << EOS
Usage: ng {start|stop|status|try-restart|condrestart|restart|usage}
JRuby Nailgun Server
EOS
}
while [ $# -gt 0 ]; do
case "$1" in
start|stop|status|try-restart|condrestart|restart|usage)
COMMAND="$1"
;;
*)
ARGS="$ARGS $1"
;;
esac
shift
done
case "$COMMAND" in
start) start ;;
stop) stop ;;
status) status ;;
condrestart|try-restart) condrestart ;;
restart) restart ;;
*) usage ;;
esac
# MIT licensed
# Copyright 2012 Barry Allard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment