我想要一個批處理文件來檢查服務「MyServiceName」是否正在運行。如果服務正在運行,我希望批處理文件禁用它,然後顯示一條消息。如果它沒有運行,並且被禁用,我希望批處理文件顯示一條消息,然後退出。謝謝您的幫助。如何通過批處理文件檢查服務是否正在運行,如果服務未運行,請將其停止?
4
A
回答
9
sc query MyServiceName| find "RUNNING" >nul 2>&1 && echo service is runnung
sc query MyServiceName| find "RUNNING" >nul 2>&1 || echo service is not runnung
要停止服務:
net stop MyServiceName
1
如果試圖拿出一個小腳本,它利用了SC
-command,這似乎也儘管一些侷限性的(我無法測試它):
@echo off
setlocal enabledelayedexpansion
:: Change this to your service name
set service=MyServiceName
:: Get state of service ("RUNNING"?)
for /f "tokens=1,3 delims=: " %%a in ('sc query %service%') do (
if "%%a"=="STATE" set state=%%b
)
:: Get start type of service ("AUTO_START" or "DEMAND_START")
for /f "tokens=1,3 delims=: " %%a in ('sc qc %service%') do (
if "%%a"=="START_TYPE" set start=%%b
)
:: If running: stop, disable and print message
if "%state%"=="RUNNING" (
sc stop %service%
sc config %service% start= disabled
echo Service "%service%" was stopped and disabled.
exit /b
)
:: If not running and start-type is manual, print message
if "%start%"=="DEMAND_START" (
echo Start type of service %service% is manual.
exit /b
)
:: If start=="" assume Service was not found, ergo is disabled(?)
if "%state%"=="" (
echo Service "%service%" could not be found, it might be disabled.
exit /b
)
我不知道這是否給出你想要的行爲。好像SC
沒有列出被禁用的服務。但是因爲如果它被禁用,你不想做任何事情,如果找不到服務,我的代碼就會打印一條消息。
但是,您可以將我的代碼作爲框架/工具箱用於您的目的。
編輯:
鑑於npocmaka的答案,你很可能改變for
-sections喜歡的東西:
sc query %service%| find "RUNNING" >nul 2>&1 && set running=true
1
此腳本採用服務名稱作爲第一個(也是唯一的)參數,或者您可以將其硬編碼到SVC_NAME作業中。 sc命令的輸出被丟棄。我不知道你是否真的想看到它。
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET SVC_NAME=MyServiceName
IF NOT "%~1"=="" SET "SVC_NAME=%~1"
SET SVC_STARTUP=
FOR /F "skip=1" %%s IN ('wmic path Win32_Service where Name^="%SVC_NAME%" get StartMode') DO (
IF "!SVC_STARTUP!"=="" SET "SVC_STARTUP=%%~s"
)
CALL :"%SVC_STARTUP%" "%SVC_NAME%"
CALL :StopService "%SVC_NAME%"
GOTO :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:"Boot"
:"System"
:"Auto"
:"Manual"
@ECHO Disabling service '%~1'.
sc.exe config "%~1" start= disabled > NUL
IF NOT ERRORLEVEL 1 @ECHO Service '%~1' disabled.
EXIT /B
:"Disabled"
@ECHO Service '%~1' already disabled.
EXIT /B
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:StopService
SETLOCAL
SET SVC_STATE=
FOR /F "skip=1" %%s IN ('wmic path Win32_Service where Name^="%~1" get State') DO (
IF "!SVC_STATE!"=="" SET "SVC_STATE=%%~s"
)
CALL :"%SVC_STATE%" "%~1"
EXIT /B
:"Running"
:"Start Pending"
:"Continue Pending"
:"Pause Pending"
:"Paused"
:"Unknown"
@ECHO Stopping service '%~1'.
sc.exe stop "%~1" > NUL
IF NOT ERRORLEVEL 1 @ECHO Service '%~1' stopped.
EXIT /B
:"Stop Pending"
:"Stopped"
@ECHO Service '%~1' is already stopping/stopped.
EXIT /B
相關問題
- 1. 如何通過批處理文件檢查服務是否正在運行並啓動它(如果未運行)?
- 2. 批處理文件檢查系統服務是否正在運行
- 3. 批處理程序停止正在運行的服務
- 4. 檢查服務是否正在運行?
- 5. 檢查服務是否正在運行
- 6. 停止服務,運行批處理文件,然後啓動服務
- 7. Selenium 2:如何檢查服務器正在運行並停止服務器?
- 8. 如果Windows進程在服務停止時正在運行,如何檢查Python?
- 9. 如何在遠程服務器上運行批處理文件
- 10. 檢查SQL服務器服務是否正在運行
- 11. 停止Windows服務是通過批處理文件
- 12. 如何在Linux上檢查服務是否正在運行Mint
- 13. 如何在安裝前檢查服務是否正在運行?
- 14. 如何檢查批處理文件是否在Windows上運行?
- 15. C程序 - 如何檢查Web服務是否正在運行
- 16. android如何檢查服務是否正在運行
- 17. 如何檢查Redis服務器是否正在運行
- 18. 如何檢查服務是否正在運行
- 19. 如何檢查硒服務器是否正在運行?
- 20. 檢查是否有其他批處理文件仍在運行
- 21. 如何檢查服務是否停止
- 22. 如何停止運行服務?
- 23. 如何通過批處理腳本檢查進程是否正在運行
- 24. Windows服務來運行批處理文件,InstallUtil /我阻止服務啓動?
- 25. 如何檢查服務器是否運行超過15分鐘?
- 26. 停止並通過批處理或cmd文件啓動服務?
- 27. .Net運行時錯誤停止服務;未處理的IOException
- 28. 批處理文件檢查是否有多個名稱中包含常用詞的服務正在運行並啓動它們(如果未運行)?
- 29. 試圖將批處理文件作爲Windows服務運行
- 30. 將批處理文件作爲Windows服務運行
'sc query state = all'(等號後的空格是importart)將獲得所有服務。 – mojo
好吧,我明白了,thx。然而,當你尋找一個特定的名字時,'SC'無論如何都會返回'STATE = STOPPED',所以你可能會測試''%state%「==」STOPPED「'。 – marsze