启动时启动java进程并在死亡时自动重新启动
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的机器(旧RedHat)上的守护程序函数直到执行的程序返回后才返回。因此,您将需要让您的小实用程序脚本进行分叉。
尝试像这样编写您的实用程序:
它是如何工作的。将命令放在括号中会启动在新进程中运行的代码。您将该进程置于后台,以便原始进程无需等待即可返回。
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:
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.
您需要一个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..