java -XX:ErrorFile=/path/to/file
to specify the path of fatal error log file.-XX:OnError
option to execute a command when an error causes on a Java VM.I have tried to set the Java VM option like this:
java -XX:ErrorFile=/path/to/error.log -XX:OnError="mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log" -jar /path/to/application.jar
But when an error occurs on Java VM, the timestamp on the filename is the time of the Java VM's startup, rather than the time of occurrence of the fatal error.
Is there any way to add the timestamp of the fatal error occurrence to the Java fatal error log filename?
I found a solution. To make the command expansion works later, you have to pass $
as a literal (use \$(...)
) like:
java -XX:ErrorFile=/path/to/error.log -XX:OnError="mv /path/to/error.log /path/to/error_store/error_\$(date +%F-%H%M%S).log" -jar /path/to/application.jar
and when a fatal error occurred, the following command was executed:
# An error report file with more information is saved as:
# /path/to/error.log
#
# -XX:OnError="mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log"
# Executing /bin/sh -c "mv /path/to/error.log /path/to/error_store/error_$(date +%F-%H%M%S).log" ...
Aborted