I've daemonized a Java program using the Commons Daemon / JSVC library and am able to successfully start/stop one instance of my service. What I really need to do is to be able to launch multiple instances of my service, starting and stopping each with different command arguments.
Probably not relevant to this problem but a little background.. my service incorporates an HTTP listener which is bound to a specific port. Each instance will be initialized to listen to a different port.
My issue is I'm unable to launch more than a single instance of my Java class using the built in functionality Commons Daemon provides. Maybe I'm missing something. I'm a longtime Windows/C# developer but relatively new to Java/Linux/Shell-scripting.
The shell script to launch the JSVC process and start/stop my daemon is below. It's what I found on this site in another post, with a few minor modifications. It passes through some command arguments my daemon requires, and I call this sh script from separate start and stop scripts that specify these parameters.
#!/bin/sh
# Setup variables
EXEC=/usr/bin/jsvc
JAVA_HOME=/usr/lib/jvm/java-7-oracle
CLASS_PATH="/usr/share/java/commons-daemon-z.0.15.jar":"/opt/LuckyElephant/lib/LuckyElephant.jar"
CLASS=co.rightside.luckyelephant.Main
USER=ubuntu
PID=/tmp/luckyelephant.pid
LOG_OUT=/tmp/luckyelephant.out
LOG_ERR=/tmp/luckyelephant.err
ARGS="$*"
do_exec()
{
$EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS $ARGS
}
case "$1" in
start)
do_exec
;;
stop)
do_exec "-stop"
;;
restart)
if [ -f "$PID" ]; then
do_exec "-stop"
do_exec
else
echo "service not running, will do nothing"
exit 1
fi
;;
*)
echo "usage: luckyelephant {start|stop|restart}" >&2
exit 3
;;
esac
If launching more than one instance of a unique Java class in not possible in JSVC, what is an alternative? I need a safe and stable way to fire up multiple instances of this service (I'll be doing it remotely and programatically using SSH), and each instance needs to shut down gracefully when completed due to being bound to a TCP port.