开发者

Restoring/Restarting a java daemon from crash

开发者 https://www.devze.com 2022-12-25 16:41 出处:网络
I am running a java app as daemon on a linux machine using a customized shell script. Since I am new to java and linux, I want to know is it possible that the app itself resurrects itself(just like r

I am running a java app as daemon on a linux machine using a customized shell script.

Since I am new to java and linux, I want to know is it possible that the app itself resurrects itself(just like restart) and recovers from cases like app crashing, u开发者_高级运维nhandled exceptions or out of memory etc.

thanks in advance

Ashish Sharma


The JVM is designed to die when there is an unrecoverable error. The ones you described fall in this category.

You could, however, easily write a shell script or a Python script that checks if the process is alive, and if it is dead, waits a few seconds and revive it. As a hint to doing this, the Unix command "pgrep" is your friend, as you can check for the exact command line used to fire a JVM (and thus including the starting class file). This way, you can determine if that specific JVM instance is running, and restart it.

All that being said, you may want to add some reporting or logging capability and check if often, because it is too easy to assume that things are ok when in fact the daemon is dying every few minutes. Make sure you've done what you could to prevent it from dying before resurrecting it.


There are Wrappers that can handle that, like Java Service Wrapper (Be aware, that the Community Edition ist under GPL) and some alternatives, mentioned here


To be honest, relaunching the daemon without any question after a crash is probably not a good idea; well it depends greatly on the type of processing achieved by your daemon, but if for example it processes files from a given directory, or requests coming from a queue manager, and the file / message contains some unexpected data causing the crash, relaunching the daemon would make it crash again immediately (excepting when the file / message is removed no matter it has been correctly processed or not, but as well it seems not to be a good idea).

In short, it's probably better to track down the possible crash reasons and fix them when possible (or at least log the the problem and go ahead, provided that the log message would ever be scanned to warn at last a human being, so some action can be engaged upon such "failures").

Anyway if you have very good reasons to do such, a solution even simpler than "checking that the process is alive" (as it would probably in some way involve some "ps -blahblah" stuff), you could just put the java program launching in a shell "while true" loop as follows :

while true
do
  # launch the java program here, no background
  # when crashing, the shell will be given hand back
  java -classpath blahblah...
  echo "program crashed, relaunching it..."
done


On unix based systems, you may use "inittab" to specify the program. If process dies, it is re-started by OS.(respawn)


I am not sure if the app itself can handle such crashes. You could write a shell script in linux which could be running as a cron job itself to manage the app, checking if the java app is running on scheduled intervals and if not, it will restart it automatically.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号