windows 用于检查系统服务是否正在运行的批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5940539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch file to check if a system service is running
提问by James
I would like to know how to check if a service is running using a batch file
我想知道如何使用批处理文件检查服务是否正在运行
e.g.
例如
if xxxx service is running go to start stage2.bat else go to echo Service not running
如果 xxxx 服务正在运行,则转到启动 stage2.bat 否则转到 echo Service not running
Any help would be appreciated
任何帮助,将不胜感激
Thanks
谢谢
回答by John Leehey
Similar to How to check if a process is running via a batch script
EDIT:
From the post, with an added else statement:
编辑:
从帖子中,添加了 else 语句:
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" (
call stage2.bat
) else (
echo Program is not running
)
For a service:
对于服务:
sc query "ServiceName" | find "RUNNING"
if "%ERRORLEVEL%"=="0" (
call stage2.bat
) else (
echo Program is not running
)
回答by PA.
read this article http://support.microsoft.com/kb/251192and see SC /?
阅读这篇文章http://support.microsoft.com/kb/251192并查看SC /?
then try
然后尝试
SC QUERY
SC QUERY
EDIT:to automate the check, pipe the result to FIND and look for RUNNING
编辑:要自动检查,将结果通过管道传送到 FIND 并查找 RUNNING
SC QUERY %1 | FIND "STATE" | FIND "RUNNING" >nul
IF ERRORLEVEL 1 (echo NOT RUNNING ) ELSE (echo RUNNING)
回答by Logan78
@echo off
color 1F
@sc query >%COMPUTERNAME%_START.TXT
ECHO REPORT MISSING INSTALL SERVICES >%COMPUTERNAME%_MISSING.TXT
find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul
IF ERRORLEVEL 1 NET START "AcPrfMgrSvc"
IF ERRORLEVEL 1 ECHO AcPrfMgrSvc >>%COMPUTERNAME%_MISSING.TXT
回答by Overdrive
My solution, because under Windows7 just IF ERRORLEVEL 1
does not work and errorlevel is 0 in case findstr
successful or not.
我的解决方案,因为在 Windows7 下只是 IFERRORLEVEL 1
不起作用,findstr
无论成功与否,错误级别为 0 。
In my case, I'm looking for something started by java.exe, lets say HELLO.jar[parameter of java.exe]
就我而言,我正在寻找由java.exe启动的东西,比如说HELLO.jar[java.exe 的参数]
wmic PROCESS LIST FULL | findstr /I java.exe | findstr /I HELLO.jar
if ErrorLevel 1 (
Echo OK
msg "%username%" HELLO.jar not started
Pause
) else (
Echo ERR
msg "%username%" HELLO.jar already running
Pause
exit
)
回答by Blackkatt
First of all you might need admin privileges and non of the examples deals with that. If you don't make us of nircmd already you could start now
首先,您可能需要管理员权限,并且没有示例处理此问题。如果你还没有让我们使用 nircmd 你现在就可以开始
This is how I do it anyways. When my bluetooth stops working
反正我就是这样做的。当我的蓝牙停止工作时
set _ServiceName=CSRBtAudioService
call :SrvStat %_ServiceName%
goto :SomeWhere
:SrvStat
sc query "%1" | find "RUNNING"
if %Errorlevel% EQU 0 ( echo: restarting %1 & nircmd elevatecmd service restart %1
) else ( echo: starting %1 & nircmd elevatecmd service start %1 )
exit /b
回答by Mike Q
In case someone is looking to do this on a remote system using SC. Note: Placed in the extra 'if /I "%%H"' statement for example purposes of further use.
如果有人希望在使用 SC 的远程系统上执行此操作。注意:为了进一步使用的示例,放置在额外的 'if /I "%%H"' 语句中。
SC \host query
Example:
例子:
:: This example prints the status of a remote service
SC \%REMOTE_SYSTEM% query %SERVICE% | FIND "STATE" >nul
IF ERRORLEVEL 1 (
echo RESULT: %SERVICE% is [UNKNOWN]
) ELSE (
for /F "tokens=3 delims=: " %%H in ('SC \%REMOTE_SYSTEM% query "%SERVICE%" ^| findstr " STATE"') do (
if /I "%%H"=="STOPPED" (
echo RESULT: %SERVICE% is %%H
) else (
echo RESULT: %SERVICE% is %%H
)
)
)