我正在编写一个 Windows 批处理脚本来安装服务。首先,我需要查找该服务是否已经存在。如果服务存在,它必须检查状态。如果状态正在运行,则必须停止并删除服务。
这是我的代码:test.bat。我正在从命令行运行它。
for /F "tokens=3 delims=: " %%H in ('sc query "IBMLibertyProfile" ^| findstr "STATE" ') do (
if /I "%%H" EQ "RUNNING" (
sc stop "IBMLibertyProfile"
)
)
我收到错误:
C:>test1.bat EQ此时出乎意料。
C:> if /I "%H" EQ "RUNNING" (
如何解决这个错误?
尝试这个:
@rem If the service doesn't exist, exit.
@sc query IBMLibertyProfile > NUL 2>&1
@if %ERRORLEVEL% neq 0 @exit /b 0
@rem If the service is already stopped, delete it.
@sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
@if %ERRORLEVEL% neq 0 @goto :DeleteService
@rem No matter it's state, tell it to stop.
@sc stop IBMLibertyProfile
@rem Wait for it to stop.
@set _WaitLoopCount=0
:StoppedWait
@if _WaitLoopCount equ 10 @goto :ServiceWaitTimeout
@timeout /t 3 > NUL 2>&1
@sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
@if %ERRORLEVEL% neq 0 @goto :StoppedWait
@rem Delete the service and exit.
:DeleteService
@sc delete IBMLibertyProfile
@exit /b 0
:ServiceWaitTimeout
@echo Service failed to reach the STOPPED state. Reboot the computer and try again.
注意:如果您的服务表现不佳,脚本可能会挂起。我将让您来解决如何处理 sc 删除失败的问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)