批处理文件检查系统服务是否正在运行
问题描述:
答
类似How to check if a process is running via a batch script
编辑:
从帖子中,添加了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
)
对于服务:
sc query "ServiceName" | find "RUNNING"
if "%ERRORLEVEL%"=="0" (
call stage2.bat
) else (
echo Program is not running
)
答
阅读这篇文章http://support.microsoft.com/kb/251192看到SC /?
然后尝试
SC QUERY
编辑: 自动检查,PI PE结果发现和寻找RUNNING
SC QUERY %1 | FIND "STATE" | FIND "RUNNING" >nul
IF ERRORLEVEL 1 (echo NOT RUNNING) ELSE (echo RUNNING)
答
@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
答
我的解决办法,因为Windows7的下只是IF ERRORLEVEL 1
不起作用,错误级别为0的情况下,findstr
成功与否。
就我而言,我正在寻找的东西通过的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
)
我已经找到了解决办法,任何人否则有这个问题: – James 2011-05-09 18:12:02
回显关闭 sc查询PeerDistSvc 如果%errorlevel%== 1060回声无法启动服务。 如果%errorlevel%== 0 echo服务已成功启动。 回声Errorlevel:%errorlevel% – James 2011-05-09 18:12:30