#!/bin/bash
# Title : {stop|start|restart} Tomcat . Default is "restart".
# Author : Cheng PJ
# E-mail : 7looki@gmail.com
# Version : 1.0
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
file_name_f1=`echo ${0} | awk -F / '{print $1}'`
file_name_f2=`echo ${0} | awk -F / '{print $NF}'`
file_pwd=`echo ${0} | sed 's#'/${file_name_f2}'$##g'`
if [ -z "${file_name_f1}" ] || [ ${file_name_f1} != ${file_name_f2} ]; then
cd ${file_pwd}
fi
tomcat_bin=`pwd`
if [ ! -f ${tomcat_bin}/startup.sh ] || [ ! -f ${tomcat_bin}/bootstrap.jar ] || [ ! -f ${tomcat_bin}/catalina.sh ]; then
echo ""
echo "This script must be in the directory under \${Tomcat_Home}/bin !"
echo ""
exit 1
fi
tomcat_whoami=`whoami`
tomcat_own_user=`ls -l ${tomcat_bin}/startup.sh | awk '{print $3}'`
tomcat_who_run=`ps -ef | grep ${tomcat_bin} | grep -v "grep\|${0}" | awk '{print $1}'`
tomcat_who_run_other=`ps -ef | grep ${tomcat_bin} | grep -v "grep\|${0}" | awk '{print $1}' | grep -v "${tomcat_who_run}\|root" | sort | uniq`
tomcat_run_num=`ps -ef | grep ${tomcat_bin} | grep -v "grep\|${0}" | wc -l`
tomcat_echo_stop () {
echo "Tomcat Stopping ... [OK]"
echo ""
}
tomcat_echo_start () {
echo ""
echo "Tomcat Starting ... [OK]"
}
tomcat_echo_error () {
echo ""
echo "Tomcat Stopped ERROR ! Please check privilege or something !"
echo ""
exit 1
}
tomcat_stop () {
if [ ${tomcat_who_run} == ${tomcat_whoami} ] || [ ${tomcat_whoami} == "root" ]; then
ps -ef | grep ${tomcat_bin} | grep -v "grep\|${0}" | awk '{print $2}' | xargs kill -9
if [ $? -eq 0 ]; then
tomcat_echo_stop;
else
tomcat_echo_error;
fi
else
echo "ERROR ! You must root or ${tomcat_who_run} to run this script !"
exit 1
fi
}
tomcat_start () {
if [ ${tomcat_own_user} == ${tomcat_whoami} ] || [ ${tomcat_own_user} == "root" ]; then
sh ${tomcat_bin}/startup.sh
if [ $? -eq 0 ]; then
tomcat_echo_start;
else
tomcat_echo_error;
fi
else
echo "ERROR ! You must root or ${tomcat_own_user} to run this script !"
exit 1
fi
}
tomcat_shutdown () {
if [ ${tomcat_run_num} -eq 0 ]; then
echo "Tomcat is not running!"
echo ""
elif [ ${tomcat_run_num} -eq 1 ]; then
tomcat_stop;
else
if [ ${tomcat_who_run_other} == "" ]; then
tomcat_stop;
else
echo "Please shutdown Tomcat with other users (${tomcat_who_run_other}) "
echo "Tomcat is not stopped !"
exit 1
fi
fi
}
tomcat_startup () {
tomcat_run_check=`ps -ef | grep ${tomcat_bin} | grep -v "grep\|${0}" | wc -l`
if [ ${tomcat_run_check} -eq 0 ]; then
tomcat_start;
else
echo "Tomcat is not stopped ! Please stop Tomcat at first !"
echo 1
fi
}
case "$1" in
start|-start|--start)
tomcat_startup
;;
stop|-stop|--stop)
tomcat_shutdown
;;
help|-help|--help)
echo ""
echo "This script used for {start|stop|restart} Tomcat !"
echo ""
echo " By ST.7looki"
echo " 7looki@gmail.com"
echo ""
;;
restart|-restart|--restart|*)
tomcat_shutdown
sleep 1
tomcat_startup
esac
posted on 2013-01-30 10:48
David1228 阅读(1482)
评论(2) 编辑 收藏 所属分类:
应用服务器