因为经常需要部署Tomcat服务器,所以才有了这个脚本.

#!/bin/bash

###############################################################
#
#author: Zhiyong Yang
#date: 2015-12-4
#email: emacsist@qq.com
#
###############################################################


TOMCAT_HOME_DIR='/home/yang/Java/apache-tomcat-7.0 (copy).63'

function stop(){
	if checkIsExist ; then
		"${TOMCAT_HOME_DIR}"/bin/shutdown.sh
		sleep 1
		while checkIsExist ; do
			sleep 2
			kill -0 $(getPID)
		done
	fi
}

function getPID(){
	# replace the grep content with your tomcat path, start with []
	local PID=`ps aux | grep "[a]pache-tomcat-7.0 (copy).63" | awk '{print $2}'`
	echo $PID
}

function start(){
	if ! checkIsExist ; then
		"${TOMCAT_HOME_DIR}"/bin/startup.sh
		tail -f -n 100 "${TOMCAT_HOME_DIR}"/logs/catalina.out
	fi
}


function checkIsExist(){
	local P_ID=$(getPID)
	if [ ! -z ${P_ID} ]; then
		echo "found running tomcat pid = ${P_ID}"
		return 0
	else
		echo "not found running tomcat : ${TOMCAT_HOME_DIR}"
		return 1
	fi
}

function usage(){
	echo "Usage:"
	echo "$0 start : for start tomcat, no repeat"
	echo "$0 stop : for stop tomcat"
	echo "$0 restart : for restart"
}

if [ -z $1 ];then
	usage
	exit 0
fi

if [ $1 = "stop" ]; then
	stop
elif [ $1 = "start" ]; then
	start
elif [ $1 = "restart" ]; then
	stop
	start
else
	usage
fi