参考/etc/rc.d/init.d/smb编写的单Tomcat起停rc脚本

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#! /bin/sh

# chkconfig: 35 96 04
# description: Apache-Tomcat service script.

# Version:
#	2008-01-17-01
# By:
#	Typhoon.Free.Wolf.

# Service name.
SVC_NAME="Apache-Tomcat"
# Lock file.
LOCK_FILE="/var/lock/subsys/tfw_tomcat"
# Installation prefix.
APP_HOME="/ext/usr/prog/Tomcat"
# The process name to monitor.
APP_PROC="java"
# Authorize user.
APP_USER="tomcat_user"

SLEEP_START=5
SLEEP_STOP=$SLEEP_START

# Importing functions.
flg_func=0
if [ -f /etc/init.d/functions ] ; then
	. /etc/init.d/functions
	flg_func=1
elif [ -f /etc/rc.d/init.d/functions ] ; then
	. /etc/rc.d/init.d/functions
	flg_func=1
fi

# Templete.
# Return:
#	FLG_PROC_ON means process is there.
#	FLG_PROC_OFF means process not exists.
#	Other means error.
FLG_PROC_ON=1
FLG_PROC_OFF=0
probe() {
	srch_rst=$(ps -A|grep $APP_PROC|awk '{print $4}')
	for rst in $srch_rst ; do
		if [ $APP_PROC = $rst ] ; then
			return $FLG_PROC_ON
		fi
	done
	return $FLG_PROC_OFF
}

# Templete.
#	Shows OK.
# Return:
echo_ok() {
	case $flg_func in
		1)
			echo_success
			echo ""
		;;
		*)
			echo ""
			echo "	[  OK  ]"
		;;
	esac
}

# Templete.
#	Shows Passed.
# Return:
echo_pass() {
	case $flg_func in
		1)
			echo_passed
			echo ""
		;;
		*)
			echo ""
			echo "	[PASSED]"
		;;
	esac
}

# Templete.
#	Shows Failed.
# Return:
echo_bad() {
	case $flg_func in
		1)
			echo_failure
			echo ""
		;;
		*)
			echo ""
			echo "	[FAILED]"
		;;
	esac
}

# Templete.
#	Start section.
# Return:
_start() {
	probe
	proc=$?
	echo ""
	echo "Starting $SVC_NAME..."

	if (touch $LOCK_FILE) ; then
		echo_ok
	else
		echo_bad
	fi
}

start() {
	probe
	proc=$?
	echo ""
	echo "Starting $SVC_NAME..."

	if [ $FLG_PROC_OFF = $proc ] ; then
		if (su - $APP_USER -c "$APP_HOME/bin/startup.sh") ; then
			sleep $SLEEP_START
			if (touch $LOCK_FILE) ; then
				echo_ok
			else
				echo_pass
				echo ""
			fi
		else
			echo_bad
		fi
	elif [ $FLG_PROC_ON = $proc ] ; then
		if (touch $LOCK_FILE) ; then
			echo_passed
			echo ""
		else
			echo_bad
		fi
	fi
}

# Templete.
#	Stop section.
# Return:
_stop() {
	probe
	proc=$?
	echo ""
	echo "Stopping $SVC_NAME..."

	if (rm -Rf $LOCK_FILE) ; then
		echo_ok
	else
		echo_bad
	fi
}

stop() {
	probe
	proc=$?
	echo ""
	echo "Stopping $SVC_NAME..."

	if [ $FLG_PROC_OFF = $proc ] ; then
		if (rm -Rf $LOCK_FILE) ; then
			echo_passed
			echo ""
		else
			echo_bad
		fi
	elif [ $FLG_PROC_ON = $proc ] ; then
		if (su - $APP_USER -c "$APP_HOME/bin/shutdown.sh") ; then
			sleep $SLEEP_STOP
			if (rm -Rf $LOCK_FILE) ; then
				echo_ok
			else
				echo_pass
				echo ""
			fi
		else
			echo_bad
		fi
	fi
}

# Parse command line parameters.
case $1 in
	start)
		start
	;;
	stop)
		stop
	;;
	restart)
		stop
		start
	;;
	*)
		# Print help
		echo "Usage: $0 {start|stop|restart}"
		exit 1
	;;
esac

exit 0