[LINUX] Set Tomcat to restart automatically when it raises an OutOfMemoryError

Memory leaks occur. However, I would like to avoid being notified that the site has gone down during a long vacation, so I investigated the method.

1: Prepare a batch file that you want to execute automatically when OutOfMemoryError occurs.

Example: Create a file restartTomcat.sh like ↓ and place it in an appropriate location (/ usr / local / tomcat / bin, etc.).



#Restart tomcat (example) If there is a command you want to execute other than restart, write it together. Like skipping emails.
systemctl restart tomcat.service

2: Add XX: OnOutOfMemoryError to the jvm option of Tomcat.

Add the following option to the environment variable CATALINA_OPTS to specify the command to execute on OutOfMemoryError.

One way to specify the tomcat jvm option is If you put a file called "setenv.sh" in / usr / local / tomcat / bin, it will be read from catalina.sh when Tomcat starts.



export CATALINA_OPTS="$CATALINA_OPTS -XX:OnOutOfMemoryError=\"/usr/local/tomcat/bin/restartTomcat.sh\""

This will run restartTomcat.sh on OutOfMemoryError.

3: HeapDump is also output.

If you also want to output HeapDump, add the following two options to the environment variable CATALINA_OPTS.

--- XX: + HeapDumpOnOutOfMemoryError (HeapDump is output at the time of OutOfMemoryError.) --- XX: HeapDumpPath (Specify the output destination of HeapDump above.)



export CATALINA_OPTS="$CATALINA_OPTS -XX:+HeapDumpOnOutOfMemoryError"
export CATALINA_OPTS="$CATALINA_OPTS -XX:HeapDumpPath=/usr/local/tomcat/logs"
export CATALINA_OPTS="$CATALINA_OPTS -XX:OnOutOfMemoryError=\"/usr/local/tomcat/bin/restartTomcat.sh\""

Recommended Posts

Set Tomcat to restart automatically when it raises an OutOfMemoryError
When it is troublesome to set up an SMTP server locally when sending mail with Python.
A memorandum when I tried to get it automatically with selenium
Be careful when adding an array to an array