Halt batch file until website stop is complete?

2019-07-17 04:24发布

This question is very similar to Halt batch file until service stop is complete? but I would like to stop a website instead. I currently have appcmd stop sites "siteName" in the script, but the batch file just issues the stop command and continues. I would like for the batch file to wait until the site is stopped before continuing. I'm running Windows Server 2008.

3条回答
我只想做你的唯一
2楼-- · 2019-07-17 04:59

I could not figure out how to get this to work using a batch file. Something like this might work:

:LoopWhileNotStopped

for /f "delims=" %x in ('appcmd list site "siteName"') do set siteInfo="%x"

IF NOT %siteInfo:~-16% == "state:Stopped)"
    GOTO :LoopWhileNotStopped

But I couldn't figure out how to modify the set siteInfo="%x" command so that it would work for the output of the appcmd command. The problem seemed to be that the appcmd output included double quotes and that interfered with command interpretation when the variable expansion is performed.

Here's a PowerShell script that should work:

C:\Windows\System32\inetsrv\appcmd.exe stop site "siteName"

do { Start-Sleep -s 1; $siteInfo = C:\Windows\System32\inetsrv\appcmd.exe list site "siteName" } until ($siteInfo -match "state:Stopped")
查看更多
闹够了就滚
3楼-- · 2019-07-17 05:01

Does appcmd return immediately no matter what? This might work, depending on how appcmd behaves.

start /wait "site stop" appcmd stop sites "siteName"

If appcmd is a script, you could probably fix it so that it doesn't return immediately.

What web server are you using?

查看更多
三岁会撩人
4楼-- · 2019-07-17 05:07

Obviously you are using IIS 7 or newer if you are using APPCMD.

  1. I think you mean SITE rather than SITES.
  2. You might have another file name APPCMD in your path. Type APPCMD /? to see if you are executing what you think.
  3. Try this to be sure you are executing the desired APPCMD:

    "%windir%\system32\inetsrv\appcmd.exe" stop site "siteName"

查看更多
登录 后发表回答