1

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.

AstroCB
  • 12,337
  • 20
  • 57
  • 73

3 Answers3

0

You can just run your java program with & at the end to have it run in the background.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
  • Forgive me if I don't already know the obvious. Does the sub-shell launched with the & symbol terminate when the primary shell is exited? Also, how would would one gracefully terminate a background Java process in such a way that the request can be captured in code and some cleanup performed? – captaintrips Jan 31 '14 at 21:44
  • 1. Yes the process terminates when the JVM exits. 2. Many servers (like tomcat and jetty) listen on a "shutdown" port and wait for a specific data (like the string 'SHUTDOWN') to get sent on that port, then they execute all the clean-up code an quit. – Ted Bigham Jan 31 '14 at 22:03
0

Assuming n instances of this daemon need to be deployed, let's say 3 (jDaemon_A, jDaemon_B & jDaemon_C). I would approach the problem using one of the following 3 options:

Option A: Have the service deployed 3 times each using a separate data directory. And use a naming convention to differentiate between each e.g.

jDaemon_A using directory /root/jdA;
jDaemon_B using directory /root/jdB;
jDaemon_C using directory /root/jdC;

such that you can start each individual one

service jDaemon_A start
etc…

Option B: Implement the above, plus another Java class which will manage as many instances as you like on top of the above such that you can have arguments passed:

service jDaemon_Manager start all
service jDaemon_Manager start B

Providing the ability to start all or just 1 particular instance only.

Option C: Multi-threading. Have you considered having the current daemon running just 1 instance with in multithreaded-mode such that the same logic is run simultaneously on separate threads? This way you would only have one daemon running.

This could be a viable solution since threads may work relatively independently.

Of course this might not be applicable in your case but thought it’s a route worth considering.

gc_i
  • 41
  • 1
  • 6
0

I have been able to run multiple instance of a daemon by using a different pidfile for each instance. And also different log_out and log_err files for easier tracking.

notsiddhartha
  • 356
  • 3
  • 6