Windows 批处理文件,等待命令完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40193625/
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
Windows batch file, wait for command to finish?
提问by Vincent Tang
I can't seem to figure this out.
我似乎无法弄清楚这一点。
I am creating a live site and deploying it online
我正在创建一个实时站点并在线部署它
batch file:
批处理文件:
START cmd /K "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"
START /wait "" http://localhost:4000/
The thing is running the command hexo generate & hexo serve --draft
takes about 5-10 seconds, time varies. Ideally I want to wait for this to occur before going to live site at http://localhost:4000
事情是运行命令hexo generate & hexo serve --draft
大约需要 5-10 秒,时间会有所不同。理想情况下,我想在访问http://localhost:4000 的实时站点之前等待这种情况发生
Some reason this windows batch command just automatically opens up localhost:4000 right away though
出于某种原因,此 Windows 批处理命令会立即自动打开 localhost:4000
回答by peter
I would use /B to stay in the same process and /wait tot wait until the first command is finished. You don't need the /wait in the second line unless there are more commands to follow. If this doens't work, experiment with leaving the cmd /K away. Since runas is an executable and the batch waits until it is finished it's possible you can let away the start command all together.
我将使用 /B 保持在同一进程中,并使用 /wait tot 等待第一个命令完成。除非有更多命令要遵循,否则您不需要第二行中的 /wait 。如果这不起作用,请尝试将 cmd /K 放在一边。由于 runas 是一个可执行文件,并且批处理会一直等到它完成,因此您可以一起放弃 start 命令。
If all this doesn't work insert 5 ping commands, that is the classic way to wait for ± one second.
如果这一切都不起作用,请插入 5 个 ping 命令,这是等待 ± 一秒的经典方法。
START /B /wait "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"
START /B "" http://localhost:4000/
回答by Compo
I'll take a stab at this, (completely untested).
我会尝试一下,(完全未经测试)。
@Echo Off
(Set SrcDir=%UserProfile%\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io)
If Not Exist "%SrcDir%\" Exit/B
Start "" /D"%SrcDir%" /Wait /B RunAs /User:administrator^
"Cmd /C Start /Wait hexo generate & Start hexo serve --draft"
Start http://localhost:4000/