在OutOfMemoryError Linux上自动重启jar [英] Auto restart jar on OutOfMemoryError Linux
问题描述
如何在Linux中自动重新启动可运行的jar文件.
How can i auto-restart the runnable jar file in Linux.
我正在单独的屏幕上在linux VPS中运行jar,但是由于OUTOFMEMORYERROR Java堆空间,它在一段时间后停止了运行.
I am running jar in linux VPS in a separate screen but it stops after some time due to OUTOFMEMORYERROR Java heap space.
推荐答案
编写一个简单的启动器,一旦启动,它将启动应用程序.像这样:
Write a simple launcher which will restart the application once it came down. Something like this:
#!/bin/sh
TEMPFILE=`mktemp`
while true ; do
echo "`date` Starting application" >> $TEMPFILE
java -XX:OnOutOfMemoryError="kill -9 %p" -jar application.jar
sleep 5
done
为了确保VM正确完成,您可能需要在主循环中考虑以下内容:
Just to be sure that the VM comes done correcly, you might want to consider the following around your main loop:
try {
// main loop
businessLogic();
} catch (OutOfMemoryError E) {
System.exit(1);
}
我个人成功地使用了 Java Service Wrapper 来立即重新启动,然后使apache tomcat失败,重新部署了太多应用程序后,内存泄漏就遭受了痛苦.您可能想看看它,这很简单.
I've personally succesfully used the Java Service Wrapper for restarting a now and then failing apache tomcat which suffered from memory leaks after applications have been redeployed too much. You might want to take a look at it, it's pretty straight forward.
这篇关于在OutOfMemoryError Linux上自动重启jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!