Halt batch file until service stop is complete?

后端 未结 7 1288
南笙
南笙 2020-12-25 10:40

I\'m using a batch file to stop a Windows service. I\'m using the sc command, but I\'m open to other ideas, given the question below. The problem is that the

7条回答
  •  别那么骄傲
    2020-12-25 11:19

    I had a similar issue with net stop. It returns when the service is indeed stopped, but the executable may still be running.
    For upgrade reasons, this is not good enough, we need to wait until the process has finished.

    I've used a simple loop to wait until the service executable has actually finished:

    net stop "ServiceName"
    :loop1
    set _CmdResult=""
    for /f "tokens=1" %%a in ('TASKLIST ^| FINDSTR ServiceName.exe') do set _CmdResult=%%a
    if not %_CmdResult% == ""  (
      timeout 5
      goto loop1
    )
    

    Once the the executable finishes, the loop will break.

提交回复
热议问题