Java Jar作为守护进程运行不能发出exec()

时间:2011-08-17 14:54:14

标签: java linux jar daemon runtime.exec

我有一个我编写的java套接字服务器,允许我保持web集群代码库同步。当我从shell登录中运行init.d脚本时如此

[root@web11 www]# /etc/init.d/servermngr start

注销并且一切正常但是如果服务器重新启动或者我使用像这样的服务运行init.d

[root@web11 www]# service servermngr start

传递给套接字服务器的任何exec()命令都不会在linux框中执行。我假设它与没有真正shell的JVM有关。如果我登录并运行

[root@web11 www]# /etc/init.d/servermngr start

...并注销所有运行不错的所有CVS命令都被执行。

另一个注意事项是作为服务运行时,套接字服务器会响应状态检查,因此它正在运行

这是init.d脚本

#!/bin/sh
# chkconfig: 2345 95 1
# description: Starts Daemon Using ServerManager.jar.
# 
# Source function library.
. /etc/init.d/functions


start () {
    echo -n $"Starting ServerManager: "

    # start daemon
    cd /www/servermanager/
    daemon java -jar ServerManager.jar > /www/logs/ServerManager.log &
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/cups
    echo "";
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n $"Stopping $prog: "
    kill `ps uax | grep -i "java -jar ServerManager.ja[r]" | head -n 1 | awk '{print $2}'`
    RETVAL=$?
    echo "";
    return $RETVAL
}

restart() {
    stop
    start
}

case $1 in
    start)
        start
    ;;
    stop)
        stop
    ;;
    *)

    echo $"Usage: servermngr {start|stop}"
    exit 3
esac

exit $RETVAL

Java负责实际执行代码:

// Build cmd Array of Strings
            String[] cmd = {"/bin/sh", "-c", "cd /www;cvs up -d htdocs/;cvs up -d phpinclude/"};
            final Process process;
            try {
                process = Runtime.getRuntime().exec(cmd);

                BufferedReader buf = new BufferedReader(new InputStreamReader(
                        process.getInputStream()));

                // Since this is a CVS UP we return the Response to PHP
                if(input.matches(".*(cvs up).*")){
                    String line1;
                    out.println("cvsupdate-start");
                    System.out.println("CVS Update" + input);
                    while ((line1 = buf.readLine()) != null) {
                        out.println(line1);
                        System.out.println("CVS:" + line1);
                    }
                    out.println("cvsupdate-end");
                }

            } catch (IOException ex) {
                System.out.println("IOException on Run cmd " + CommandFactory.class.getName() + " " + ex);
                Logger.getLogger(CommandFactory.class.getName()).log(Level.SEVERE, null, ex);
            }

获取任何帮助

4 个答案:

答案 0 :(得分:0)

您尝试运行的命令是什么? cd不是一个程序,如果你有;,你有多个命令。你只能运行一个程序!

答案 1 :(得分:0)

您是否以root身份启动了该过程?系统上运行的是什么版本的(bash?)?你可能想给csh一个旋转只是为了排除shell本身的问题。我还建议用'&'链接命令代替 ';'。最后,您可能会发现创建一个包含所有命令的shell脚本更容易,并且由java进程调用。您可能还想调查nohup并检查/ etc / security / limits

答案 2 :(得分:0)

您可能更乐意使用http://akuma.kohsuke.org/来帮助您解决这些问题,或者至少使用Apache Commons Exec。

答案 3 :(得分:0)

如果有人遇到问题,这是修复我的问题的启动脚本

#!/bin/sh
# chkconfig: 2345 95 1
# description: Starts Daemon Using ServerManager.jar.
# 
# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="ServerManager"
servermanager="java"
serveroptions=" -jar ServerManager.jar"
pid_file="/var/run/servermanager.pid"

launch_daemon()
{
  /bin/sh << EOF
     java -Ddaemon.pidfile=$pid_file $serveroptions <&- &
     pid=\$!
     echo \${pid}
EOF
}

start () {
    echo -n $"Starting $prog: "
 if [ -e /var/lock/subsys/servermanager ]; then
                if [ -e /var/run/servermanager.pid ] && [ -e /proc/`cat /var/run/servermanager.pid` ]; then
                echo -n $"cannot start: servermanager is already running.";
                failure $"cannot start: servermanager already running.";
                echo
                return 1
                fi
        fi
    # start daemon
    cd /www/voodoo_servermanager/
    export CVSROOT=":pserver:cvsd@cvs.zzzzz.yyy:/cvsroot";
    daemon "$servermanager $serveroptions > /www/logs/ServerManager.log &"
    #daemon_pid=`launch_daemon`
    #daemon ${daemon_pid}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/servermanager && pidof $servermanager > $pid_file
    echo "";
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n $"Stopping $prog: "
        if [ ! -e /var/lock/subsys/servermanager ]; then
            echo -n $"cannot stop ServerManager:  ServerManager is not running."
            failure $"cannot stop ServerManager:  ServerManager is not running."
        echo
        return 1;
        fi
        killproc $servermanager
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/servermanager;
        return $RETVAL
}

restart() {
    stop
    start
}

case $1 in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    *)

    echo $"Usage: servermngr {start|stop|restart}"
    RETVAL=1
esac

exit $RETVAL