开发者

How do I wait for a service/process to start before continuing a batch script?

I am writing a windows batch script to uninstall some software. However I need to wait after the uninstaller has finished for a service to be restarted before continuing with the next uninstall.

I can make the script wait for the uninstaller to finsh using:-

for /f "usebackq" %%M in ('tasklist /nh /fi "imagename eq %process_1%"') do if not %%M==%ignore_result% goto 1

But I cannot for the life 开发者_JAVA百科of me figure out how to get the script to then wait for a service to start before continuing the script and running more uninstalls.

I am open to any suggestions.


What about this to avoid using an intermediate file

FOR /F "usebackq tokens=1,4" %%A IN (`sc query AcquisitionService`) DO (
    IF %%A==STATE SET serviceStatus=%%B
)


OK from comments I have used the script below to sort out the problem. I was hoping not to use a intermediate file, but when needs must....

@echo off
if exist service.txt del service.txt
set process=setup.exe
set ignore_result=INFO:
:1
for /f "usebackq" %%M in ('tasklist /nh /fi "imagename eq %process%"') do if not %%M==%ignore_result% goto 1
:2
sc query AcquisitionService>service.txt
find "RUNNING"<service.txt>nul
if errorlevel 1 goto 2
:3
echo.
echo Stuff finished.......

Thanks for the ideas.


The anwser provided by the poster works but it uses a temporary file. I found a way to avoid that using the pipe separator.

This code will start the service then wait until it has started :

sc start ServiceName
:1
sc query ServiceName>NUL | find "RUNNING">NUL
if errorlevel 1 goto 1
(...)

The rediection (>NUL) are optional, or at least they don't make a difference in my case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜