Tomcat Local Page Control Script

Working with a large number of connections, the Tomcat service freezes or does not respond correctly.
Below is a simple script that is configured as a cronjob, allowing the automatic restart of the Tomcat web service.

Tomcat Local Page Control Script

apache-tomcat

Create a file named tomcat_check.sh and paste the following code:

~:$ sudo vi /usr/local/bin/tomcat_check.sh

#!/bin/sh
# www.garanet.net
# Definisco variabili

HOST=127.0.0.1
PORT=8080
PAGE=pagetocheck.html

# Loop di controllo
while [ 1 ]
do
    # Provo ad accedere alla pagina di TOMCAT
    RES=`wget -O - -o /dev/null --proxy=off http://${HOST}:${PORT}/${PAGE} | awk '{ print $1 }'`
    echo got ${RES}

    # Decido la risposta
if [ "$RES" = "YES" ]
then
    echo Tomcat non risponde su $HOST:$PORT
else
    echo Tomcat sembra stoppato.
    echo Riavvio...
    for thepin in `ps -Af | grep -v grep | grep tomcat | grep catalina | awk '{ print $2 }'`
    do
        kill -9 ${thepin}
    done
    echo Starting...
    sudo -u tomcat /etc/init.d/tomcat start
fi

sleep 60
done

Assign execute privileges to the newly created file:

~:$ sudo chmod a+x /usr/local/bin/tomcat_check.sh

You can insert the following script into a cronjob.

~:$ sudo crontab -e