10

I have tomcat server running. and it crashes suddenly for some reason... however I am trying to find the error.

Is there a function in tomcat or java like beforeExit() or ifCrashed() which I can override and write some code there like notifying myself if server crashes for some reason.

xenteros
  • 15,586
  • 12
  • 56
  • 91
sanjeevprasad
  • 804
  • 1
  • 6
  • 21
  • You tagged tomcat7 and tomcat8. Which one are you really using? Same for java. – Turtle Aug 02 '17 at 09:13
  • actually currently on tomcat7 but soon I will upgrade to tomcat8 if it exist on that version. – sanjeevprasad Aug 02 '17 at 09:18
  • In java there is the [Thread.setUncaughtExceptionHandler](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)) where you can register a method to be called if there is an uncought exception. I donot know if this works with tomcat, too – k3b Aug 02 '17 at 10:26
  • 1
    If Tomcat crashes because of an `Exception` or `Error`, you should be able to find it in the logs. If it crashes because of some native bug, you should be able to find a `hs_err_pid.log` crash log. The [Oracle Java Troubleshooting Guide, Appendix C](http://www.oracle.com/technetwork/java/javase/felog-138657.html#gbwcy) specifies where to find crash logs. – Hendrik Aug 09 '17 at 20:49

4 Answers4

7

You could try using shut down hooks which are executed on system exit. It's not guaranteed though that it will be executed on a hard crash like SIGKILL but this may be an option in some cases:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        // Implementation goes here..;
    }
});
Danylo Zatorsky
  • 5,856
  • 2
  • 25
  • 49
2

I don't think that crashed server can manage something. You can consider this steps:

  • use servletContextListeners in order to run code before servlet context will shutdown. It will work if shutdown is done gracefully
  • if it is JVM that is crushing you can try to find tools that provide JVM monitoring services and reporting. Also I recommend to provide flag in order to get reports from OutOfMemoryError and open JMX ports for monitoring
  • you can right some mock rest endpoint and run some external job that will call that endpoint periodically and if it returns HTTPStatus.OK then it working, if not - then you can report yourself someway.

It could be better if you can provide logs and some information about what is really happening with server. Maybe here people will help but I gues this is topic of another questions.

Izbassar Tolegen
  • 1,990
  • 2
  • 20
  • 37
0

In case of a fatal crash, you will get a jvm log. You can specify the location by giving tomcat a -XX:ErrorFile on the Oracle JVM

http://www.oracle.com/technetwork/java/javase/felog-138657.html

If it is not that fatal, you can adjust the log settings in tomcat to display better logging levels, such as specified on their doc site

https://tomcat.apache.org/tomcat-6.0-doc/logging.html

If it dies because of out of memory errors, you can get a heap dump on OOM, and analyse it with i.e. jhat

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
0

If profiling is an option for you JProfiler has an offline profiling mode where you can capture information about the running program and log it for viewing at a later time. Check out the docs JPRofiler Offline Profiling

This might give you some more insight into whats happening in the application. According to this documentation you can also manage the profiling agent from you application.

bombadil
  • 5
  • 4