改进的 Tomcat 起停脚本
Page: [index.html] (v2017-04-15_19-10)
用 ps 搜索 Tomcat 进程的特征字符串,以此确认 $CATALINA_HOME 下 (而不是其他地方) 的
Tomcat (而不是其他 java 进程) 是否正在运行,并进一步决定 start 、stop 、restart
、restop 时的动作和显示内容:
#! /bin/sh
# chkconfig: 35 96 04
# description: Apache-Tomcat service script.
# Version:
# 2017-05-28_21-10
# By:
# Typhoon.Free.Wolf.
# ========================================
# MANDATORY variables with CUSTOMIZED values:
SVC_NAME="Apache-Tomcat";
LOCK_FILE="/var/lock/subsys/tfw_tomcat";
START_WAIT_SECONDS=8;
STOP_WAIT_SECONDS=2;
# ========================================
# CUSTOMIZED variables:
APP_HOME="/ext/ro1/prog/Tomcat";
APP_USER="typhoon";
# PROC_FILTER_CMD="ps aux | grep -v \"grep\" | grep \"catalina.base=$APP_HOME\";";
# PROC_FILTER_CMD="ps aux";
# ========================================
# MANDATORY Functions with CUSTOMIZED inner content:
_status() {
# echo "$PROC_FILTER_CMD";
# $("$PROC_FILTER_CMD");
# `"$PROC_FILTER_CMD"`;
ps aux | grep -v "grep" | grep "catalina.base=$APP_HOME";
return $?;
}
_start() {
su - "$APP_USER" -c "\"$APP_HOME/bin/startup.sh\"";
return $?;
}
_stop() {
su - "$APP_USER" -c "\"$APP_HOME/bin/shutdown.sh\"";
return $?;
}
# ========================================
# Static common part.
ARG_TMP=$2;
if [ -n "$ARG_TMP" ]; then
# echo "$? Not null, to check whether a number.";
printf %f "$ARG_TMP" 2>/dev/null 1>&2;
if [ "$?" -eq 0 ]; then
# echo "$? Number printable, to check value.";
if [ $(echo "${ARG_TMP} > 0" | bc) = 1 ]; then
START_WAIT_SECONDS="$ARG_TMP";
echo "Starting wait second(s) overrided to $START_WAIT_SECONDS.";
fi;
fi;
fi;
ARG_TMP=$3;
if [ -n "$ARG_TMP" ]; then
# echo "$? Not null, to check whether a number.";
printf %f "$ARG_TMP" 2>/dev/null 1>&2;
if [ "$?" -eq 0 ] && [ $(echo "${ARG_TMP} > 0" | bc) = 1 ]; then
# echo "$? Number printable, to check value.";
# if [ $(echo "${ARG_TMP} > 0" | bc) = 1 ]; then
STOP_WAIT_SECONDS="$ARG_TMP";
echo "Stopping wait second(s) overrided to $STOP_WAIT_SECONDS.";
# fi;
fi;
fi;
# Source function library.
FUNCTION_LIB_FLAG="Y";
if [ -f "/etc/init.d/functions" ] ; then
. "/etc/init.d/functions";
elif [ -f "/etc/rc.d/init.d/functions" ] ; then
. "/etc/rc.d/init.d/functions";
else
FUNCTION_LIB_FLAG=Y;
fi;
# echo $FUNCTION_LIB_FLAG;
# Template.
# Shows OK.
# Return:
echo_ok() {
case $FUNCTION_LIB_FLAG in
Y)
echo_success;
echo "";
;;
*)
echo "";
echo " [ OK ]";
;;
esac
}
# Template.
# Shows Passed.
# Return:
echo_pass() {
case $FUNCTION_LIB_FLAG in
Y)
echo_passed;
echo "";
;;
*)
echo "";
echo " [PASSED]";
;;
esac
}
# Template.
# Shows Failed.
# Return:
echo_bad() {
case $FUNCTION_LIB_FLAG in
Y)
echo_failure;
echo "";
;;
*)
echo "";
echo " [FAILED]";
;;
esac
}
status() {
echo "Service [ $SVC_NAME ] status:";
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
echo "$STAT: Process running...";
else
echo "$STAT: Process not found.";
fi;
return "$STAT";
}
start() {
echo "Service [ $SVC_NAME ] starting...";
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
STAT=1;
echo "Already / still running, unable to start again!";
echo_pass;
else
_start; STAT=$?;
if [ $STAT -eq 0 ]; then
echo "Waiting $START_WAIT_SECONDS second(s) for starting...";
sleep $START_WAIT_SECONDS;
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
STAT=0;
echo "Running, successfully started.";
touch "$LOCK_FILE"; STAT=$?;
if [ $STAT -eq 0 ]; then
echo_ok;
else
STAT=1;
echo "Failed creating lock-file \"$LOCK_FILE\".";
echo_pass;
fi;
else
STAT=2;
echo "Not running, failed starting.";
echo_bad;
fi;
else
STAT=3;
echo "Failed starting.";
echo_bad;
fi;
fi;
return "$STAT";
}
stop() {
echo "Service [ $SVC_NAME ] stopping...";
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
_stop; STAT=$?;
if [ $STAT -eq 0 ]; then
echo "Waiting $STOP_WAIT_SECONDS second(s) for stopping...";
sleep $STOP_WAIT_SECONDS;
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
STAT=2;
echo "Still running, failed stopping.";
echo_bad;
else
STAT=0;
echo "Not running, successfully stopped.";
rm -rf "$LOCK_FILE"; STAT=$?;
if [ $STAT -eq 0 ]; then
echo_ok;
else
STAT=1;
echo "Failed removing lock-file \"$LOCK_FILE\".";
echo_pass;
fi;
fi;
else
STAT=3;
echo "Failed stopping.";
echo_bad;
fi;
else
STAT=1;
echo "Not running / already stopped, unable to stop again!";
echo_pass;
fi;
return "$STAT";
}
restart() {
stop; STAT=$?;
if [ $STAT -eq 0 -o $STAT -eq 1 ]; then
start; STAT=$?;
fi;
return "$STAT";
}
restop() {
start; STAT=$?;
if [ $STAT -eq 0 -o $STAT -eq 1 ]; then
stop; STAT=$?;
fi;
return "$STAT";
}
RETVAL=0;
case "$1" in
stat)
status;RETVAL=$?;
;;
status)
status;RETVAL=$?;
;;
start)
start;RETVAL=$?;
;;
stop)
stop;RETVAL=$?;
;;
restart)
restart;RETVAL=$?;
;;
restop)
restop;RETVAL=$?;
;;
*)
echo $"Usage: $0 {stat|status|start|stop|restart|restop}";
RETVAL=2;
esac;
exit $RETVAL;
# ========================================
#! /bin/sh
# chkconfig: 35 96 04
# description: Apache-Tomcat service script.
# Version:
# 2017-04-04_12-50
# By:
# Typhoon.Free.Wolf.
# ========================================
# Customized values:
SVC_NAME="Apache-Tomcat";
START_WAIT_SECONDS=10; #1;
STOP_WAIT_SECONDS=2; #1;
# ========================================
# Customized variables:
APP_HOME="/ext/ro1/prog/Tomcat";
APP_USER="typhoon";
# PROC_FILTER_CMD="ps aux | grep -v \"grep\" | grep \"catalina.base=$APP_HOME\";";
# PROC_FILTER_CMD="ps aux";
# ========================================
# Functions with customized inner content:
_status() {
# echo "$PROC_FILTER_CMD";
# $("$PROC_FILTER_CMD");
# `"$PROC_FILTER_CMD"`;
ps aux | grep -v "grep" | grep "catalina.base=$APP_HOME";
return $?;
}
_start() {
su - "$APP_USER" -c "\"$APP_HOME/bin/startup.sh\"";
return $?;
}
_stop() {
su - "$APP_USER" -c "\"$APP_HOME/bin/shutdown.sh\"";
return $?;
}
# ========================================
# Static common part.
expr "$2" "+" 0 2>/dev/null;
if [ "$?" -eq 0 ]; then
START_WAIT_SECONDS="$2";
fi;
expr "$3" "+" 0 2>/dev/null;
if [ "$?" -eq 0 ]; then
STOP_WAIT_SECONDS="$3";
fi;
# Source function library.
FUNCTION_LIB_FLAG="Y";
if [ -f "/etc/init.d/functions" ] ; then
. "/etc/init.d/functions";
elif [ -f "/etc/rc.d/init.d/functions" ] ; then
. "/etc/rc.d/init.d/functions";
else
FUNCTION_LIB_FLAG=Y;
fi;
# echo $FUNCTION_LIB_FLAG;
# Template.
# Shows OK.
# Return:
echo_ok() {
case $FUNCTION_LIB_FLAG in
Y)
echo_success;
echo "";
;;
*)
echo "";
echo " [ OK ]";
;;
esac
}
# Template.
# Shows Passed.
# Return:
echo_pass() {
case $FUNCTION_LIB_FLAG in
Y)
echo_passed;
echo "";
;;
*)
echo "";
echo " [PASSED]";
;;
esac
}
# Template.
# Shows Failed.
# Return:
echo_bad() {
case $FUNCTION_LIB_FLAG in
Y)
echo_failure;
echo "";
;;
*)
echo "";
echo " [FAILED]";
;;
esac
}
status() {
echo "Service [ $SVC_NAME ] status:";
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
echo "$STAT: Process running...";
else
echo "$STAT: Process not found.";
fi;
return "$STAT";
}
start() {
echo "Service [ $SVC_NAME ] starting...";
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
STAT=1;
echo "Already / still running, unable to start again!";
echo_pass;
else
_start; STAT=$?;
if [ $STAT -eq 0 ]; then
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
STAT=0;
echo "Running, successfully started.";
echo_ok;
else
STAT=2;
echo "Not running, failed starting.";
echo_bad;
fi;
else
STAT=3;
echo "Failed starting.";
echo_bad;
fi;
fi;
return "$STAT";
}
stop() {
echo "Service [ $SVC_NAME ] stopping...";
_status; STAT=$?;
if [ $STAT -eq 0 ]; then
_stop; STAT=$?;
if [ $STAT -eq 0 ]; then
sleep $STOP_WAIT_SECONDS;
_status;STAT=$?;
if [ $STAT -eq 0 ]; then
STAT=2;
echo "Still running, failed stopping.";
echo_bad;
else
STAT=0;
echo "Not running, successfully stopped.";
echo_ok;
fi;
else
STAT=3;
echo "Failed stopping.";
echo_bad;
fi;
else
STAT=1;
echo "Not running / already stopped, unable to stop again!";
echo_pass;
fi;
return "$STAT";
}
restart() {
stop; STAT=$?;
if [ $STAT -eq 0 -o $STAT -eq 1 ]; then
start; STAT=$?;
fi;
return "$STAT";
}
restop() {
start; STAT=$?;
if [ $STAT -eq 0 -o $STAT -eq 1 ]; then
if [ $STAT -eq 0 ]; then
sleep $START_WAIT_SECONDS;
fi;
stop; STAT=$?;
fi;
return "$STAT";
}
RETVAL=0;
case "$1" in
stat)
status;RETVAL=$?;
;;
status)
status;RETVAL=$?;
;;
start)
start;RETVAL=$?;
;;
stop)
stop;RETVAL=$?;
;;
restart)
restart;RETVAL=$?;
;;
restop)
restop;RETVAL=$?;
;;
*)
echo $"Usage: $0 {stat|status|start|stop|restart|restop}";
RETVAL=2;
esac;
exit $RETVAL;
# ========================================
遗留问题:
在脚本中无论使用“$("$PROC_FILTER_CMD")”还是“`"$PROC_FILTER_CMD"`”,均报“ps command not found”,无法将进程探测命令参数化。