我看过这篇帖子..
Batch runs manually but not in scheduled task
它不是Win 2008,而是服务器2003
准时计划任务..
不运行。无论是预定还是“要求”手动运行。
运行.. d:\ WORK \脚本\ res_tc.bat
之前使用双冒号“::”之前已经遇到过问题并试图使用:REM而不是 - 但没有区别。
运行方式: NT AUTHORITY \ SYSTEM
@ECHO OFF
CLS
REM DISABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /DISABLE
ECHO CHECKING FOR AND KILLING JAVA
TASKLIST | FIND /I "java.exe" && IF NOT ERRORLEVEL 1 TASKKILL /F /IM java.exe
REM LOOK FOR TOMCAT..
REM If Tomcat was not installed using the installer - instead using a batch file
REM We have to look for the service instead
REM We cannot depend on the Registry key - HKLM\SOFTWARE\Apache Software Foundation\Tomcat
REM Instead, we have to look for it using the registry key
REM HKLM\SYSTEM\CurrentControlSet\services
REM However, we know Tomcat - could be tomcat5, tomcat6, or tomcat7
FOR /F "usebackq tokens=1" %%a in (`reg query "HKLM\SYSTEM\CurrentControlSet\services" ^| find /i "tomcat"`) do SET TomcatService=%%a
REM THANKS FOR THIS - https://stackoverflow.com/questions/17279114/split-path-and-take-last-folder-name-in-batch-script
set TomcatServiceName1=%TomcatService:~0,-1%
for %%f in (%TomcatServiceName1%) do set TomcatServiceName=%%~nxf
FOR /F "usebackq tokens=3" %%a in (`reg query "%TomcatService%" /v ImagePath`) do SET TomcatFolder=%%a
FOR /F "tokens=1-2,* delims=\" %%1 IN ("%TomcatFolder%") do Set TomcatLocation= %%1\%%2
REM Stop Service: Apache Tomcat
ECHO STOPPING SERVICE Apache Tomcat
NET STOP %TomcatServiceName%
ECHO CHECKING TO INSURE SERVICE Apache Tomcat IS STOPPED
NET START | FIND /I "Apache Tomcat"
IF NOT ERRORLEVEL 1 ECHO Apache Tomcat IS STARTED... STOPPING NOW
IF NOT ERRORLEVEL 1 NET STOP %TomcatServiceName%
ECHO DOUBLE-CHECKING TO INSURE SERVICE Apache Tomcat IS STOPPED
PING 127.0.0.1 -w 1 > NUL
NET START | FIND /I "Apache Tomcat"
IF ERRORLEVEL 1 ECHO Apache Tomcat IS STOPPED... GOOD TO GO
IF NOT ERRORLEVEL 1 ECHO Apache Tomcat IS STARTED...
REM -- SOME FAILURE IN STOPPING THE SERVICE, YOU WILL WANT TO EXIT
IF NOT ERRORLEVEL 1 GOTO END
PING 127.0.0.1 -w 1000 > NUL
REM DELETE LOGS - YOU DON'T WANT TO DELETE LOGS IN A PRODUCTION ENVIRONMENT
ECHO DEL Tomcat Log files
ECHO Y | DEL %TomcatLocation%\aa*.*
ECHO Y | DEL %TomcatLocation%\logs\*.*
ECHO DEL Log files
ECHO Y | DEL D:\logs\*.*
ECHO Y | DEL D:\bo_logs\*.*
REM DELETE FOLDER: %TomcatLocation%\work (Catalina Work Directory)
ECHO DELETING THE %TomcatLocation%\work DIRECTORY
RD /S/Q %TomcatLocation%\work
REM DELETE FOLDER: %TomcatLocation%\temp (Temp Directory)
ECHO DELETING THE %TomcatLocation%\temp DIRECTORY
RD /S/Q %TomcatLocation%\temp
IF NOT EXIST %TomcatLocation%\temp\. MD %TomcatLocation%\temp
REM START SERVICE: APACHE TOMCAT
ECHO STARTING Apache Tomcat
NET START | FIND /I "Apache Tomcat"
IF ERRORLEVEL 1 NET START %TomcatServiceName%
IF NOT ERRORLEVEL 1 ECHO Apache Tomcat IS ALREADY STARTED
PING 127.0.0.1 -w 1000 > NUL
ECHO.
ECHO PROCESS IS COMPLETE - VISUALLY DOUBLE CHECK TO INSURE
ECHO Apache Tomcat
ECHO ARE STARTED
REM ENABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /ENABLE
GOTO END
:END
rem pause
EXIT
安排在计划任务中使用的另一个考虑因素..您可能希望坚持使用ARGS(参数),以便不会“自动”调用文件。
考虑..
SET /A ARGS_COUNT=0
FOR %%A in (%*) DO SET /A ARGS_COUNT+=1
REM ECHO %ARGS_COUNT%
If %ARGS_COUNT% == 0 GOTO END
If %ARGS_COUNT% == 1 GOTO CONTINUE
:CONTINUE
REM DISABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /DISABLE
...
在您的预定任务中,修改“运行”(注意击球后的空格和1)。 D:\ WORK \ Scripts \ res_tc.bat 1
然后,您必须从命令提示符运行批处理文件,如下所示:res_tc.bat 1或类似的东西..如果您只运行没有ARGS的res_tc.bat,则fille将结束而不执行。希望这会有所帮助。
注意:这个下一部分不是推荐用于计划任务,而是用于独立文件。如果你想要相对快速和可靠的方法来停止/启动Apache Tomcat,你可以执行以下操作(靠近顶部 - 注意添加CHOICE):
@ECHO OFF
CLS
CHOICE /C YN /M "DO YOU WANT TO RESTART APACHE TOMCAT WEB SERVICE?"
IF %ERRORLEVEL% == 2 GOTO END
REM DISABLE TOMCAT CHECK
SCHTASKS /QUERY|find /i "TCCheck">NUL
IF NOT ERRORLEVEL 1 SCHTASKS /CHANGE /TN "TCCheck" /DISABLE
...
答案 0 :(得分:1)
ECHO CHECKING FOR AND KILLING JAVA
TASKLIST | FIND /I "java.exe" && IF NOT %ERRORLEVEL% 1 TASKKILL /F /IM java.exe
if not %errorlevel% 1 ...
不正确。正确的用法是
if not errorlevel 1 ...
if not %errorlevel%==1 ...