#!/bin/sh
# FileName: tomcat-service.sh
# Description: tomcat service,the script needs to be placed in the tomcat directory
# Date: 2020-07-31
# Author: 123
# Version: v1.0
export JAVA_HOME=/usr/java/jdk1.8.0_111
export JRE_HOME=/usr/java/jdk1.8.0_111/jre
export CLASSPATH=/usr/java/jdk1.8.0_111/lib
export PATH=$JAVA_HOME/bin:$PATH
tomcatPath=$(cd `dirname $0`; pwd)
startupPath="$tomcatPath"/bin/startup.sh
logPath="$tomcatPath"/logs/tomcat-service.log
operation="$1"
monitorUrl=http://localhost:8787/examples/
#pageInfo="$tomcatPath"/logs/tomcat-service-interface.info
pageInfo=/dev/null
tomcat_pid() {
echo `ps aux|grep "$tomcatPath/bin"|grep -v grep|awk '{print $2}'`
}
status() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "[info]Tomcat is running with pid:$pid"
else
echo "[info]Tomcat is not running"
fi
return 0
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "[info]Tomcat is already running (pid:$pid)"
else
echo "[info]Starting Tomcat..."
echo "[info]Startup Script:$tomcatPath/bin/startup.sh"
sh $startupPath
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "[info]Stoping Tomcat..."
echo "[info]Killing the processes $pid of $tomcatPath"
kill -9 $pid
else
echo "[info]Tomcat is not running"
fi
return 0
}
monitor() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "[info]Tomcat is running with pid:$pid"
returnCode=$(curl -s -o $pageInfo -m 600 --connect-timeout 600 $monitorUrl -w %{http_code})
if [ $returnCode -eq 200 ]
then
echo "[info]status code: $returnCode,tomcat interface access is normal"
else
echo "[error]status code: $returnCode,Access error,The error log has been output to $pageInfo"
echo "$(jmap -heap $pid)"
echo "[info]start to restart tomcat"
stop
sleep 3
start
status
fi
else
echo "Tomcat is not running"
start
status
fi
return 0
}
run() {
case "$operation" in
start)
echo "[info][$(date +'%F %H:%M:%S')] start $tomcatPath"
start
status
;;
stop)
echo "[info][$(date +'%F %H:%M:%S')] stop $tomcatPath"
stop
status
;;
restart)
echo "[info][$(date +'%F %H:%M:%S')] restart $tomcatPath"
stop
sleep 3
start
status
;;
status)
echo "[info][$(date +'%F %H:%M:%S')] status $tomcatPath"
status
;;
monitor)
echo "[info][$(date +'%F %H:%M:%S')] monitor $tomcatPath"
monitor
;;
'')
echo "[info]Usage: $0 {status|start|stop|restart|monitor}"
;;
*)
esac
}
run | tee -a $logPath