windows 下tomcat重启脚本

@echo off&setlocal enabledelayedexpansion
@rem (start /min cmd.exe /c %0 :&exit)
start /min 
title Tomcat 重启脚本%1
cd ..set CATALINA_HOME=%cd%
set STR_TASK=%1
set JAVA_HOME=%CATALINA_HOME%\bin\jdk1.7.0_55
cd /D %CATALINA_HOME%\bin
call %CATALINA_HOME%\bin\shutdown.bat
ping -n 3 localhost >nul
wmic process where name="java.exe" get processid,commandline |findstr /i %STR_TASK%  >#
for /f "delims=*" %%i in (#) do (      
   set var=%%i
   set var=!var:start= #!
   for /f "tokens=3 delims=#" %%a in ("!var!") do (set tomcatpid=%%a)
)
del # >nul
if defined tomcatpid taskkill /pid !tomcatpid!
ping -n 3 localhost >nul
call %CATALINA_HOME%\bin\startup.bat

taskkill /im cmd.exe

由于windows的任务不好锁定,所以执行的时候,需要传入tomcat的主目录的名称用来锁定进程号,保证运行的tomcat中没有重复的文件名。

下面是我代码的调用示例

String cmdcommand = "";
            String osname = System.getProperties().getProperty("os.name").toLowerCase();
            File  partfile = new File(new  File(ServerDirUtils.getServerRoot()).getParent());
            String rootdirpath = partfile.getParent();
            String rootname = partfile.getParentFile().getName();
            if(!osname.toLowerCase().contains("windows")){
                String rootPath = rootdirpath +File.separator+"bin"+File.separator+"restart.sh";
                cmdcommand = "sh "+rootPath+   "  "+rootname;
            }else{
                String rootPath = rootdirpath+File.separator+"bin"+File.separator+"restart.bat";
                cmdcommand = "cmd /c start "+rootPath+"  "+rootname;
            }
            logger.error("执行的命令为:"+cmdcommand);
            try {
                Runtime.getRuntime().exec(cmdcommand);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

如假如我的tomcat的目录为D:\zhk\apache-tomcat-8.5.39

那命令的组成为:

cmdcommand = "cmd /c start  【你的重启bat的所在的位置,建议放在bin目录下】  apache-tomcat-8.5.39

windows 下tomcat重启脚本

上一篇:c# 如何在一般处理程序中返回json


下一篇:python subprocess模块的shell参数问题