使用批处理文件的 Tomcat7 重启问题

Tomcat7 restart issue using batch file

我已经编写了批处理文件来停止和启动 tomcat 服务器,但是当服务器负载过大时有时会失败。下面是重启的脚本-

@echo off

set current_date = %DATE%
echo Stopping Apache Tomcat service on   + %DATE%  >> restart.log
net stop tomcat7 >> restart.log 
net start tomcat7 >> restart.log

下面是我在重启失败时得到的日志-

The Apache Tomcat 7.0 Tomcat7 service could not be stopped.

The service is starting or stopping.  Please try again later.

我应该在 stop/start 之间使用时间差还是应该强制终止服务器(如果有类似情况)

如果服务无法停止,您可以尝试

@echo off

set "current_date=%DATE%" & rem not used!!

>>"restart.log" (
  echo Stopping Apache Tomcat service on %DATE%
  rem try to stop service. If failed (||) then kill process
  net stop tomcat7 2>&1 || (
    for /F "usebackq tokens=2 skip=3" %%i in (`tasklist /FI "services eq tomcat7"`) do (
      echo Failed to stop. Killing process Id %%i
      taskkill /PID %%i /F >NUL 2>&1
    )
    rem wait 1.5 second
    ping 1.1.1.1 -w 1500 -n 1 >NUL
  )
  net start tomcat7 2>&1
)