版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。 https://cloud.tencent.com/developer/article/1434696
#!/bin/bash
. /etc/rc.d/init.d/functions
TOMCAT_PORT=$1
TOMCAT=tomcat_$1
TOMCAT_HOME=/opt/tomcats/${TOMCAT}
TOMCAT_BIN=${TOMCAT_HOME}/bin
#TOMCAT_LOG=${TOMCAT_HOME/logs/catalina.$(date +%Y-%m-%d).out
TOMCAT_LOG=${TOMCAT_HOME}/logs/catalina.out
. /etc/rc.d/init.d/functions
if -f ${TOMCAT_BIN}/catalina.sh ;then
echo "${TOMCAT} is valilable !!!"
else
echo "${TOMCAT} not valilable !!!"
exit
fi
RETVAL=$?
start(){
checkrun
if [ $RETVAL -eq 0 ]; then
echo "-- Starting ${TOMCAT}... --"
bash ${TOMCAT\_BIN}/startup.sh
touch /var/lock/subsys/${TOMCAT\_PORT}
checklog
status
else
echo "-- ${TOMCAT} already running !"
fi
}
stop(){
checkrun
if [ $RETVAL -eq 1 ]; then
echo "-- Shutting down tomcat..."
${TOMCAT\_BIN}/shutdown.sh
if [ "$1" != "re" ]; then
checklog
else
sleep 5
fi
rm -rf /var/lock/subsys/${TOMCAT\_PORT}
status
else
echo "-- ${TOMCAT} not running"
fi
}
status(){
checkrun
if [ $RETVAL -eq 1 ]; then
echo -n "-- ${TOMCAT} ( pid "
ps ax --width=1000 | grep ${TOMCAT\_PORT}| grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'
echo -n ") is running..."
echo
else
echo "-- ${TOMCAT} is stopped"
fi
#echo "---------------------------------------------"
}
log(){
status
checklog yes
}
kill(){
checkrun
if [ $RETVAL -eq 1 ]; then
read -p "-- Do you really want to kill ${TOMCAT\_PORT} progress?[no])" answer
case $answer in
Y|y|YES|yes|Yes)
ps ax --width=1000 | grep ${TOMCAT\_PORT} | grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'| xargs kill -9
status
;;
\*);;
esac
else
echo "-- exist with ${TOMCAT\_PORT} still running..."
fi
}
checkrun(){
ps ax --width=1000 |grep ${TOMCAT\_PORT}| grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 "\n"}' | wc -l > /var/run/tomcat\_process\_count
read line < /var/run/tomcat\_process\_count
if [ $line -gt 0 ]; then
RETVAL=1
return $RETVAL
else
RETVAL=0
return $RETVAL
fi
}
checklog(){
answer=$1
if [ "$answer" != "yes" ]; then
read -p "-- See Catalina.out log to check $2 status?[yes])" answer
fi
case $answer in
Y|y|YES|yes|Yes|"")
tail -f ${TOMCAT\_LOG}
;;
\*)
# status
# exit 0
;;
esac
}
checkexist(){
if [ ! -d ${TOMCAT\_HOME} ]; then
echo "-- ${TOMCAT\_HOME} does not exist."
exit 0
fi
}
case "$2" in
start)
checkexist
start
exit 0
;;
stop)
checkexist
stop
exit 0
;;
restart)
checkexist
stop re
start
exit 0
;;
status)
checkexist
status
#${TOMCAT\_BIN}/catalina.sh version
exit 0
;;
log)
checkexist
log
exit 0
;;
kill)
checkexist
status
kill
exit 0
;;
\*)
echo "Usage: $0 {start|stop|restart|status|log|kill}"
echo "service tomcat {8080|8081|..} {start|stop|restart|status|log|kill}"
esac
exit 0