5

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?

4

1 回答 1

7

仔细阅读 JSVC 的工作原理后:http ://commons.apache.org/daemon/jsvc.html

我发现我做错了什么,我必须使用等待参数

当使用 -wait 参数时,启动器进程会一直等待,直到控制器说“我准备好了”,否则在创建控制器进程后返回。

于 2013-01-21T18:19:12.700 回答