版权声明:作者写博是为了总结经验,和交流学习之用。 如需转载,请在文章页面明显位置给出原文连接。谢谢!如有问题,请留言! https://blog.csdn.net/changqing5818/article/details/82631134
前言
Java Web应用中如何执行脚本重启Tomcat服务,包含两部分:启停脚本、调用Java代码。
启停脚本
#!/bin/sh
#
# -----------------------------------------------------------------------------
# description: Auto-starts tomcat
# Author: ZCQ
# -----------------------------------------------------------------------------
#
# -----------------------------------------------------------------------------
# Environment Variable Prerequisites
#
# TOMCAT_HOME May point at your Catalina "build" directory.
#
# JAVA_HOME Must point at your Java Development Kit installation.
# Required to run the with the "debug" argument.
# -----------------------------------------------------------------------------
# The Return Value
RETVAL=0
# The Operator
user=`whoami`
# SHUTDOWN_WAIT is wait time in seconds for java process to stop
SHUTDOWN_WAIT=5
# The Key Word
key_word="org.apache.catalina.startup.Bootstrap"
# 取出脚本名
cmd="`echo $0 | sed -e 's/^.*\///'`"
echo "**$cmd executed by $user"
# -----------------------------------------------------------------------------
# Check The Enviroment Vair TOMCAT_HOME
# -----------------------------------------------------------------------------
function check_env()
{
if [ -z $TOMCAT_HOME ];then
echo -e "\n\e[31;1;5mThe Enviroment Vair TOMCAT_HOME is not exists.\e[0m\n"
return 1
fi
return 0
}
# -----------------------------------------------------------------------------
# Get PID Of Tomcat
# -----------------------------------------------------------------------------
tomcat_pid()
{
echo $(ps -fu $user | grep $key_word | grep $user | grep -v grep | awk '{print $2}')
}
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
start()
{
# Get The PID Of Tomcat
pid=$(tomcat_pid)
# The Process is already exists.
if [ -n "$pid" ];
then
echo -e "\n\e[31;1;5mTomcat is running now (pid: $pid)\e[0m\n"
echo -e "\e[32;1mPlease Stop Servers and try again!\e[0m\n"
RETVAL=5
else
[ -z $TOMCAT_HOME ] || RETVAL=5
[ -f $TOMCAT_HOME/bin/startup.sh ] || RETVAL=5
[ -x $TOMCAT_HOME/bin/startup.sh ] || RETVAL=5
echo -e "\e[35;1m------------------------------- Starting Tomcat Server -----------------------------------\e[0m\n"
$TOMCAT_HOME/bin/startup.sh
RETVAL=$?
echo -e "\e[35;1m--------------------------------------------------------------------------------------------\e[0m\n"
fi
return $RETVAL
}
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
stop()
{
[ -z $TOMCAT_HOME ] || RETVAL=5
[ -f $TOMCAT_HOME/bin/shutdown.sh ] || RETVAL=5
[ -x $TOMCAT_HOME/bin/shutdown.sh ] || RETVAL=5
# Get The PID Of Tomcat
pid=$(tomcat_pid)
if [ -n "$pid" ];
then
echo -e "\e[35;1m--------------------------- Stoping Tomcat Server --------------------------------\e[0m\n"
$TOMCAT_HOME/bin/shutdown.sh
RETVAL=$?
let kwait=$SHUTDOWN_WAIT
count=0
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo -n -e "\e[00;31mWaiting for Processes to exit..\e[00m\n"
sleep 1
let count=$count+1
done
if [ $count -gt $kwait ]
then
echo -e "\n\e[00;31mForce killing processes with 'kill -9 $pid' after $SHUTDOWN_WAIT seonds\e[00m\n"
kill -9 $pid
fi
echo -e "\e[35;1m-------------------------------------------------------------------------------------\e[0m\n"
else
echo -e "\n\e[00;31mTomcat is not running!\e[00m\n"
fi
return $RETVAL
}
# -----------------------------------------------------------------------------
# View the CATALINA Server Status
# -----------------------------------------------------------------------------
status()
{
# Get The PID Of Tomcat
pid=$(tomcat_pid)
if [ -n "$pid" ];
then
echo -e "\n\e[32;1mTomcat is running with pid: $pid\e[0m\n"
else
echo -e "\n\e[00;31mTomcat is not running!\e[00m\n"
fi
}
# 命令使用方式回显
function usage {
if [ -n "$*" ]; then
echo -e "\n\033[35m Unkown command: \033[0m \033[41;30m $* \033[0m\n"
else
echo -e "\n\033[35m The $cmd Lack Of Parameter.\033[0m\n"
fi
echo "[usage]: $cmd -v : for System Version Info."
echo "[usage]: $cmd start : for Start System."
echo "[usage]: $cmd stop : for Stop System."
echo "[usage]: $cmd status : for System Status"
echo "[usage]: $cmd restart : for Restart System."
echo "[usage]: $cmd blank/? : for help"
echo ""
exit 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
# fork 以子进程的方式执行
exec $TOMCAT_HOME/bin/restart.sh &> restart_tomcat.log 2>&1 &
;;
*)
usage $*
esac
RETVAL=$?
echo "$0 execute successfully on `hostname` at `date '+%Y-%m-%d %H:%M:%S'`."
exit $RETVAL
子脚本
#!/bin/sh
sh $TOMCAT_HOME/bin/tomcat.sh stop
sh $TOMCAT_HOME/bin/tomcat.sh start
Java代码
LinuxServerUtil.java
public static void restartTomcatServer() {
// 获取Tomcat路径
String tomcatHome = getTomcatHome(LinuxServerUtil.class.getResource("/").getFile());
// 重启tomcat指令
final String restartSh = "sh " + tomcatHome + "/bin/tomcat.sh restart";
logger.info("调用指令【{}】进行Tomcat重启", restartSh);
new Thread(new Runnable() {
@Override
public void run() {
try {
Runtime.getRuntime().exec(restartSh);
} catch (IOException e) {
logger.error("执行重启tomcat服务出现异常。",e);
}
}
}).start();
}
使用方式
- 将
tomcat.sh
、restart.sh
放入$TOMCAT_HOME/bin
目录下 - 配置环境变量
$TOMCAT_HOME