我正在编写一个关闭tomcat的脚本,运行一个清理文件,然后再次启动该服务。我在循环中等待我的日志文件中的特定行,对于我来说," INFO:服务器启动在"中。然后,我想转到运行部分,提示我该服务已备份并正在运行。任何建议将不胜感激。
这是我到目前为止所写的内容。我是批处理脚本的新手,所以请原谅我的语法问题。
@echo off
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "RUNNING" (
echo Service Tomcat7 is "%%H" Stopping Service now
net stop "Tomcat7"
goto :pending
)
)
:pending
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "STOP_PENDING" (
echo Service Tomcat7 is running Stopping Service is "%%H"
timeout /t 15
goto :pending
)
else(
goto :stopped
)
)
:stopped
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "STOPPED" (
echo Service Tomcat7 has "%%H"
echo Cleaning up the log files
cleanup.bat
echo ################# Starting Service Tomcat7 #################
net Start "Tomcat7"
timeout /t 15 /nobreak > NUL
goto :loop
)
)
:loop
findstr /m "INFO: Server startup in" C:\Apps\Apache\apache-tomcat-7.0.42\logs\tomcat7-stderr*
if %errorlevel%==0 (
echo Found Server Startup!
goto :running
) else (
echo Server is loading files Please wait...
timeout /t 15
goto :loop
)
:running
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "RUNNING" (
echo Service Tomcat7 is "%%H"
timeout /t 30
)
)
答案 0 :(得分:2)
call cleanup.bat
如果您不包含call
,则执行将转移到被调用的批处理文件,并且不会返回到调用者批处理文件。
答案 1 :(得分:0)
工作修复刚刚缺少调用命令!把它清理了一下。
@echo off
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "RUNNING" (
echo Service Tomcat7 is "%%H" Stopping Service now
net stop "Tomcat7"
goto pending
)
)
:pending
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "STOP_PENDING" (
echo Service Tomcat7 is running Stopping Service is "%%H"
timeout /t 15
goto pending
)
else(
goto stopped
)
)
:stopped
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "STOPPED" (
echo Service Tomcat7 has "%%H"
echo Cleaning up the log files
call cleanup.bat
echo ################# Starting Service Tomcat7 #################
net Start "Tomcat7"
timeout /t 10 /nobreak > NUL
echo loading server config
goto loading
)
)
:loading
findstr /m /c:"INFO: Server startup in" C:\Apps\Apache\apache-tomcat-7.0.42\logs\tomcat7-stderr*
if %errorlevel%==0 (
echo Found Server Startup!
goto running
) else (
echo No matches found
timeout /t 15
goto loading
)
:running
for /F "tokens=3 delims=: " %%H in ('sc query "Tomcat7" ^| findstr " STATE"') do (
if /I "%%H" equ "RUNNING" (
echo Service Tomcat7 is "%%H"
goto fin
) else (
timeout /t 15
goto running
)
)
:fin
echo ############### FINISHED ###############
timeout /t 45