#!/usr/bin/bash

#Tate:2022.3.24
#Author:Yingjian
#function: 一键启动tomcat
#env
tomcat_home=/usr/local/tomcat
tomcat_port=8080

case $1 in
start)
pidlist=`netstat -tunlp |grep $tomcat_port|awk '{print $NF}'|awk -F '/' '{print $1}'`
if [ "$pidlist" != "" ];then
echo "$tomcat_home 服务已存在,不需要启动"
exit 3
else
echo "$tomcat_home 服务不存在,正在启动"
$tomcat_home/bin/startup.sh
sleep 3
fi
;;
stop)
echo "正在关闭$tomcat_home"
$tomcat_home/bin/shutdown.sh
pidlist=`netstat -tunlp |grep $tomcat_port|awk '{print $NF}'|awk -F '/' '{print $1}'`
kill -15 $pidlist &> /dev/null
sleep 3
;;
restart)
pidlist=`netstat -tunlp |grep $tomcat_port|awk '{print $NF}'|awk -F '/' '{print $1}'`
if [ "$pidlist" != "" ];then
echo "tomcat 进程号为$pidlist 正在关闭该服务"
$tomcat_home/bin/shutdown.sh
kill -15 $pidlista &>/dev/null
sleep 5
echo "正在启动$tomcat_home"
$tomcat_home/bin/startup.sh
else
echo "tomcat进程不存在 正在重启$tomcat_home"
$tomcat_home/bin/startup.sh
fi
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 2
;;
esac
