#!/bin/bash
# Prog name : cron.tomcatRestart 1.0
# Prog by doly @ 22.03.15
# * * * * * su - root -c '/root/wellC/cron.tomcatRestart &> /dev/null'

### < User variables > ###
maxRestartThreadCnt=800
tomcatctl="/usr/local/tomcat/bin/tomcatctl"
fileLog="/root/wellC/cron.tomcatRestart.log"
### </ User variables > ###

tomcatChk=`$tomcatctl status | grep not | wc -l`
if [ $tomcatChk -eq 1 ] ; then
 now="`date '+%Y.%m.%d %H:%M:%S'`"
 echo "[ $now ] Start Done" >> $fileLog
 $tomcatctl start >> $fileLog
fi

##
threadCnt="`$tomcatctl status | awk '{print $NF}'`"
if [ "$threadCnt" -ge "$maxRestartThreadCnt" ]; then
 now="`date '+%Y.%m.%d %H:%M:%S'`"
 echo "[ $now ] $threadCnt Stopping .... " >> $fileLog
 $tomcatctl stop  >> $fileLog
 sleep 10
 $tomcatctl start  >> $fileLog
 sleep 10

 tomcatChk=`ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | wc -l`
 while [ "$tomcatChk" -eq 0 ]
 do
 $tomcatctl start
 sleep 10
 tomcatChk=`ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | wc -l`
 done

 now="`date '+%Y.%m.%d %H:%M:%S'`"
 echo "[ $now ] Restart Done" >> $fileLog
fi
