Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring/Restarting a java daemon from crash

Tags:

java

linux

shell

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, unhandled exceptions or out of memory etc.

thanks in advance

Ashish Sharma

like image 613
Ashish Sharma Avatar asked Dec 17 '22 01:12

Ashish Sharma


1 Answers

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.

like image 130
Uri Avatar answered Dec 21 '22 11:12

Uri