启动时启动java进程并在死亡时自动重新启动

发布于 2024-08-20 14:19:26 字数 1337 浏览 5 评论 0原文

我的 Java 应用程序有两个要求。如果它死了,请重新启动它。如果服务器重新启动,则重新启动它 - 很简单。使用答案此处 我有一个脚本,当java应用程序终止时它将重新启动。

#!/bin/bash

until java -Xms256m -Xmx768m -jar MyApp.jar; do
    echo "MyApp crashed with exit code $?.  Respawning... " >&2
    sleep 5
done

我可以使用“nohup restart_script.sh &”来运行它它会运行一整天而没有问题。现在是启动要求。我采用了 /etc/init.d/crond 脚本,并用我的脚本替换了 crond 二进制文件,但它在启动时挂起。

#!/bin/bash
#
# Init file for my application.
#
. /etc/init.d/functions

MYAPP=restart_script.sh
PID_FILE=/var/run/myapp.pid

start(){
        echo -n "Starting My App"
        daemon --user appuser $MYAPP
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/myapp
        return $RETVAL
}

stop(){
        echo -n "Stopping my application"
        killproc $MYAPP
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/myapp
        return $RETVAL
}

...

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
...
esac

当我运行 /sbin/service myapp start 时,脚本启动但挂起控制台。我尝试过“daemon --user appuser nohup $MYAPP &”我立即返回到提示符,没有任何 [OK] 指示,当我执行 ps 时,我仍然看到 init 挂起。有什么想法如何在 init 脚本中调用脚本并让它正确返回吗?

谢谢,

格雷格

I have two requirements for my Java app. If it dies, restart it. If the server reboots, restart it - simple enough. Using the answer here I have a script that will restart when the java application dies.

#!/bin/bash

until java -Xms256m -Xmx768m -jar MyApp.jar; do
    echo "MyApp crashed with exit code $?.  Respawning... " >&2
    sleep 5
done

I can run this with "nohup restart_script.sh &" and it will run all day long without issue. Now for the startup requirement. I took the /etc/init.d/crond script and replaced the crond binary with my script but it hangs on startup.

#!/bin/bash
#
# Init file for my application.
#
. /etc/init.d/functions

MYAPP=restart_script.sh
PID_FILE=/var/run/myapp.pid

start(){
        echo -n "Starting My App"
        daemon --user appuser $MYAPP
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/myapp
        return $RETVAL
}

stop(){
        echo -n "Stopping my application"
        killproc $MYAPP
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/myapp
        return $RETVAL
}

...

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
...
esac

When I run /sbin/service myapp start the script starts but hangs the console. I have tried "daemon --user appuser nohup $MYAPP &" and I am immediately returned to the prompt without any [OK] indication and when I do a ps, I still see that the init is hung. Any ideas how to call a script within the init script and get it to return properly?

Thanks,

Greg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-08-27 14:19:26

我的机器(旧RedHat)上的守护程序函数直到执行的程序返回后才返回。因此,您将需要让您的小实用程序脚本进行分叉。

尝试像这样编写您的实用程序:

#!/bin/bash

(
    until java -Xms256m -Xmx768m -jar MyApp.jar; do
        echo "MyApp crashed with exit code $?.  Respawning... " >&2
        sleep 5
    done
) &

它是如何工作的。将命令放在括号中会启动在新进程中运行的代码。您将该进程置于后台,以便原始进程无需等待即可返回。

The daemon function on my machine (old RedHat) does not return until the executed program returns. So you are going to need to have your little utility script do the forking.

Try writing your utility like this:

#!/bin/bash

(
    until java -Xms256m -Xmx768m -jar MyApp.jar; do
        echo "MyApp crashed with exit code $?.  Respawning... " >&2
        sleep 5
    done
) &

How this works. Putting a command in parentheses starts code running in a new process. You put the process in the background so the original process will return without waiting for it.

━╋う一瞬間旳綻放 2024-08-27 14:19:26

您需要一个java服务包装器,这是一个非常好的包装器... tanuki
我的意思是,你不需要重新发明轮子,那里有工具。

You need a java service wrapper, here is a very good one... tanuki
I mean to say, you don't need to reinvent the wheel, there are tools out there..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文