1

When the JVM crashes it generates a log file that is saved (by default) in the current folder of the application and has a name respecting the following format: hs_err_pid[PID].log

I need to make the JVM save this file in a different folder with a desired name. So, I use this command line argument for the virtual machine:

-XX:ErrorFile=./log/jvm_error_pid%p.log

It is working, but I don't like something about this solution. Let's assume the log folder already contains a file named jvm_error_pid5000.log. If there is a future crash on a JVM that has 5000 PID, then JVM does not override the jvm_error_pid5000.log file from the log folder and it saves this log file in a totally different place (from what I tested in the TEMP folder of the current OS user). It doesn't even rename the new file by appending a random string to assure uniqueness.

I haven't found anything regarding this uniqueness problem on the Oracle documentation page about crash log files. I would like to know if there is a way to improve that command line argument so that it will always generate different crash log file names. For example, I want to use the command line argument to put the date and hour in the file name:

-XX:ErrorFile=./log/jvm_error_pid%p_%d_%h.log
Atul
  • 1,536
  • 3
  • 21
  • 37
Valy
  • 573
  • 2
  • 12

2 Answers2

2

JVM does not expand placeholders in ErrorFile other than %p. But you may modify your shell script that launches Java, and use shell variables instead.

#!/bin/bash
TS=`date +%F-%H%M%S`
java -XX:ErrorFile=/tmp/hserr_%p_$TS.log ...
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Think I will just let it go with **-XX:ErrorFile=./log/jvm_error_pid%p.log** as this JVM error appears very rarely anyway (so the chance to have a duplicate is super low). I will mark this answer as this is a good idea to dynamically generate that argument somewhere before you application is started. – Valy Feb 16 '17 at 16:45
  • Another idea I would have is to implement another Java application that starts your actual Java application. This is another way to dynamically generate the **-XX:ErrorFile** argument. – Valy Feb 16 '17 at 16:49
1

one thing you can do is just email the error log to some email address

java -XX:OnError="cat hs_err_pid%p.log|mail support@acme.com" \ MyApplication

Refer http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbmum for more information.

Atul
  • 1,536
  • 3
  • 21
  • 37
  • it's not applicable in my situation, but this is an interesting idea :) – Valy Feb 15 '17 at 16:57
  • This is just a work around. So, every time when jvm crashes you will get an email and you will have log for all the crashes . – Atul Feb 15 '17 at 16:59
  • 2
    you can expand on @Leozeo's idea and make a small script to output to a file name that includes a timestamp – Radu C Feb 15 '17 at 16:59