Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Daemonizing java application with JSVC

I'm trying to daemonize my application using Apache Commons Daemon using the Daemon interface. Java application it self isn't doing anything just writes to stout.

I compiled jsvc: http://people.apache.org/~mturk/daemon-1.0.10/

(even tried newest version: http://people.apache.org/~mturk/daemon-1.0.12/ )

and wrote this basic script.

do_exec()
{
    $EXEC \
        -home "$JAVA_HOME" \
        -cp $CLASS_PATH \
        -outfile $LOG_OUT \
        -errfile $LOG_ERR \
        -pidfile $PID \
        $1 \
        $MAIN_CLASS
    echo "result: $?"
}

case "$1" in
    start)
            do_exec
            ;;
    stop)
            do_exec "-stop"
            ;;
    restart)
            do_exec "-stop"
            do_exec
            ;;
    *)
            echo "usage: daemon {start|stop|restart}" >&2
            exit 3
            ;;
esac

Now when I try to stop the daemon when it's not running I'll get response code 255. Thas fantastic.

But when I try start the daemon when it's already running I get response code 0. But in my errfile I find:

Still running according to PID file /tmp/deamon.pid, PID is 1799
Service exit with a return value of 122

Same thing when I try throw exception in start() method and attempt to start daemon, response code 0. But errfile:

 Service exit with a return value of 5

What I'm missing here? How can I inform user stat the deamon didn't start or that it is already running?

like image 603
user987220 Avatar asked Jan 20 '13 15:01

user987220


1 Answers

After carefully reading how JSVC works: http://commons.apache.org/daemon/jsvc.html

I found out what I was doing wrong, I have to use the wait parameter

When the -wait parameter is used, the launcher process waits until the controller says "I am ready", otherwise it returns after creating the controller process.

like image 198
user987220 Avatar answered Nov 08 '22 23:11

user987220