windows 如何在继续批处理脚本之前等待服务/进程启动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3004909/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:37:17  来源:igfitidea点击:

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

windowsscriptingwindows-servicesbatch-filewait

提问by Shane McD

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.

我正在编写一个 Windows 批处理脚本来卸载某些软件。但是,我需要在卸载程序完成后等待重新启动服务,然后再继续下一次卸载。

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

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 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.

我愿意接受任何建议。

回答by Gamil

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
)

回答by yhoyhoj

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

海报提供的 anwser 可以工作,但它使用临时文件。我找到了一种使用管道分隔符来避免这种情况的方法。

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.

重定向 (>NUL) 是可选的,或者至少它们对我的情况没有影响。

回答by Shane McD

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.......

@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.

谢谢你的想法。