# vim:sw=4:ts=4:et
<<INFO
AUTHOR:运维@小兵
DATE:2021-07-08
DESCRIBE:安装/卸载tomcat
SYSTEM:CentOS7/RedHat7
WARNING:
MODIFY:
INFO
set -e
source /etc/profile #避免ansible远程执行shell获取环境变量不一致的问题
WORKDIR=$(cd `dirname $0`;pwd) #脚本所在路径
echo "Current Excute: bash ${WORKDIR}/$0 $@"
TOMCAT_VERSION="8.5.59"
TOMCAT_PKG_NAME="apache-tomcat-${TOMCAT_VERSION}"
TOMCAT_USER=$2 #启动tomcat的用户
TOMCAT_DIR=$3 #tomcat目录
PID_PATH="${TOMCAT_DIR}/${TOMCAT_PKG_NAME}/tomcat.pid" #tomcat pid路径
SYSTEMD_CONFIG="/usr/lib/systemd/system/tomcat.service" #systemd管理tomcat的配置文件
#环境检查
Check_Env(){
[[ $UID -ne 0 ]] && "ERROR:Please Use Admin(root) Excute......" && exit 1
if ! id ${TOMCAT_USER} &> /dev/null;then
echo "ERROR:Tomcat User Not Found" && exit 1
fi
}
#配置Tomcat通过systemd启停
Config_Tomcat_Systemd(){
local java_bin_path=$(command -v java)
local java_home=$(echo ${java_bin_path} | awk -F'/bin/java' '{print $1}')
if [[ -f ${SYSTEMD_CONFIG} && ! -f "${SYSTEMD_CONFIG}.bak" ]];then
cp ${SYSTEMD_CONFIG}{,.bak}
fi
tee ${SYSTEMD_CONFIG} << EOF
[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=${java_home}
Environment=JRE_HOME=\$JAVA_HOME/jre
Environment=CLASS_PATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib
PIDFile=${PID_PATH}
ExecStart=${TOMCAT_DIR}/${TOMCAT_PKG_NAME}/bin/catalina.sh start
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
User=${TOMCAT_USER}
Group=${TOMCAT_USER}
WorkingDirectory=${TOMCAT_DIR}/${TOMCAT_PKG_NAME}
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
}
Install_Tomcat(){
echo "INFO:Begin Install Tomcat..."
if ! command -v java &> /dev/null;then
echo "ERROR:Please Install Jdk" && exit 1
fi
local tomcat_pid=$(lsof -i:8080 -t)
local tomcat_status=$(systemctl is-active tomcat)
if [[ -n ${tomcat_pid} && ${tomcat_status} == "active" ]];then
echo "INFO:Already Install tomcat" && exit 0
fi
[[ ! -f ${WORKDIR}/${TOMCAT_PKG_NAME}.tgz ]] && echo "ERROR:${WORKDIR}/${TOMCAT_PKG_NAME}.tgz Not Exist" && exit 1
mkdir -p ${TOMCAT_DIR}
tar xf "${WORKDIR}/${TOMCAT_PKG_NAME}.tgz" -C ${TOMCAT_DIR}
sed -i "158a CATALINA_PID=${PID_PATH}" "${TOMCAT_DIR}/${TOMCAT_PKG_NAME}/bin/catalina.sh"
chown -R ${TOMCAT_USER}.${TOMCAT_USER} ${TOMCAT_DIR}
Config_Tomcat_Systemd
systemctl enable tomcat
echo "INFO:Wait Tomcat Start..."
systemctl start tomcat
sleep 5
local tomcat_pid=$(lsof -i:8080 -t)
local tomcat_status=$(systemctl is-active tomcat)
if [[ -n ${tomcat_pid} && ${tomcat_status} == "active" ]];then
echo "INFO:Install tomcat Success" && exit 0
else
echo "ERROR:Install tomcat Fail" && exit 1
fi
}
Uninstall_Tomcat(){
echo "INFO:Begin Uninstall Tomcat..."
local tomcat_status=$(systemctl is-active tomcat)
if [[ ${tomcat_status} == "active" ]];then
systemctl stop tomcat
fi
rm -f ${SYSTEMD_CONFIG}
rm -rf ${TOMCAT_DIR}/${TOMCAT_PKG_NAME}
local pid_num=$(ps -ef | grep "${TOMCAT_DIR}/${TOMCAT_PKG_NAME}" | grep -v grep | wc -l)
if [[ ${pid_num} -eq 0 ]];then
echo "INFO:Uninstall Tomcat Success"
else
echo "ERROR:Uninstall Tomcat Fail" && exit 1
fi
}
######################主程序######################
[[ $# -ne 3 ]] && echo "ERROR:Invalid Param!!! eg:bash $0 (install|uninstall) tomcat用户 tomcat目录" && exit 1
Check_Env
case $1 in
install)
Install_Tomcat;;
uninstall)
Uninstall_Tomcat;;
*)
echo "ERROR:Invalid Param!!! eg:bash $0 (install|uninstall) tomcat用户 tomcat目录" && exit 1
esac