简体   繁体   English

使用JSVC守护Java应用程序

[英]Daemonizing java application with JSVC

I'm trying to daemonize my application using Apache Commons Daemon using the Daemon interface. 我正在尝试使用Apache Commons Daemon使用Daemon接口来守护我的应用程序 Java application it self isn't doing anything just writes to stout. 它自己的Java应用程序只是写入粗壮的东西。

I compiled jsvc: http://people.apache.org/~mturk/daemon-1.0.10/ 我编译了jsvc: http ://people.apache.org/~mturk/daemon-1.0.10/

(even tried newest version: http://people.apache.org/~mturk/daemon-1.0.12/ ) (甚至尝试过最新版本: 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. 现在,当我尝试在守护程序未运行时停止守护程序时,我会得到响应代码255.真是棒极了。

But when I try start the daemon when it's already running I get response code 0. But in my errfile I find: 但是当我尝试启动守护进程时它已经运行我得到响应代码0.但在我的错误文件中我发现:

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: 当我尝试在start()方法中抛出异常并尝试启动守护进程时,响应代码为0.但是错误文件:

 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? 如何告知用户stat deamon没有启动或者它已经运行?

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

I found out what I was doing wrong, I have to use the wait parameter 我发现我做错了什么,我必须使用wait参数

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. 当使用-wait参数时,启动程序进程将等待,直到控制器显示“我准备好”,否则在创建控制器进程后返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM