windows 如何等待进程终止以在批处理文件中执行另一个进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8177695/
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
How to wait for a process to terminate to execute another process in batch file
提问by DogDog
How to wait for a process to terminate before executing another process in a batch file? Let's say I have a process notepad.exe
that I need to kill before executing wordpad.exe
. Then, when wordpad.exe
is terminated, I need to launch notepad.exe
again. How do I do that?
如何在批处理文件中执行另一个进程之前等待进程终止?假设我有一个进程notepad.exe
需要在执行之前终止wordpad.exe
。然后,当wordpad.exe
终止时,我需要notepad.exe
再次启动。我怎么做?
采纳答案by aphoria
Try something like this...
尝试这样的事情......
@ECHO OFF
PSKILL NOTEPAD
START "" "C:\Program Files\Windows NT\Accessories\wordpad.exe"
:LOOP
PSLIST wordpad >nul 2>&1
IF ERRORLEVEL 1 (
GOTO CONTINUE
) ELSE (
ECHO Wordpad is still running
TIMEOUT /T 5
GOTO LOOP
)
:CONTINUE
NOTEPAD
I used PSLIST
and PSEXEC
, but you could also use TASKKILL
and TASKLIST
. The >nul 2>&1
is just there to hide all the output from PSLIST
. The SLEEP 5
line is not required, but is just there to restrict how often you check if WordPad is still running.
我使用了PSLIST
and PSEXEC
,但你也可以使用TASKKILL
and TASKLIST
。的>nul 2>&1
是就在那里隐藏所有从输出PSLIST
。该SLEEP 5
行不是必需的,但只是用于限制您检查写字板是否仍在运行的频率。
回答by RealHowTo
Use start /w programnameto wait for the end of programname
使用start /w programname等待 programname 结束
START /W notepad
ECHO Back from notepad
START /W wordpad
ECHO Back from wordpad
START /W notepad
回答by Mitch Buz Stringer
This is an updated version of aphoria's Answer.
这是 aphoria's Answer 的更新版本。
I Replaced PSLIST and PSEXEC with TASKKILL and TASKLIST`. As they seem to work better, I couldn't get PSLIST to run in Windows 7.
我用 TASKKILL 和 TASKLIST` 替换了 PSLIST 和 PSEXEC。由于它们似乎工作得更好,我无法让 PSLIST 在 Windows 7 中运行。
Also replaced Sleep with TIMEOUT.
还用 TIMEOUT 替换了 Sleep。
This Was everything i needed to get the script running well, and all the additions was provided by the great guys who posted the comments.
这是我让脚本运行良好所需的一切,所有的补充都是由发表评论的好人提供的。
Also if there is a delay before the .exe starts it might be worth inserting a Timeout before the :loop.
此外,如果 .exe 启动之前有延迟,则可能值得在 :loop 之前插入超时。
@ECHO OFF
TASKKILL NOTEPAD
START "" "C:\Program Files\Windows NT\Accessories\wordpad.exe"
:LOOP
tasklist | find /i "WORDPAD" >nul 2>&1
IF ERRORLEVEL 1 (
GOTO CONTINUE
) ELSE (
ECHO Wordpad is still running
Timeout /T 5 /Nobreak
GOTO LOOP
)
:CONTINUE
NOTEPAD
回答by Gabe Halsmer
I liked the "START /W" answer, though for my situation I found something even more basic. My processes were console applications. And in my ignorance I thought I would need something special in BAT syntax to make sure that the 1st one completed before the 2nd one started. However BAT appears to make a distinction between console apps and windows apps, and it executes them a little differently. The OP shows that window apps will get launched as an asynchronous call from BAT. But for console apps, that are invoked synchronously, inside the same command window as the BAT itself is running in.
我喜欢“START / W”答案,但对于我的情况,我发现了一些更基本的东西。我的进程是控制台应用程序。在我的无知中,我认为我需要一些特殊的 BAT 语法来确保第一个在第二个开始之前完成。然而,BAT 似乎对控制台应用程序和 Windows 应用程序进行了区分,并且它们的执行方式略有不同。OP 显示窗口应用程序将作为来自 BAT 的异步调用启动。但是对于控制台应用程序,它们是在 BAT 本身运行的同一命令窗口中同步调用的。
For me it was actually better not to use "START /W", because everything could run inside one command window. The annoying thing about "START /W" is that it will spawn a new command window to execute your console application in.
对我来说,实际上最好不要使用“START /W”,因为一切都可以在一个命令窗口内运行。“START /W”的烦人之处在于它会产生一个新的命令窗口来执行您的控制台应用程序。
回答by sql xdetails
This works and is even simpler. If you remove ECHO-s, it will be even smaller:
这有效,甚至更简单。如果删除 ECHO-s,它会更小:
REM
REM DEMO - how to launch several processes in parallel, and wait until all of them finish.
REM
@ECHO OFF
start "!The Title!" Echo Close me manually!
start "!The Title!" Echo Close me manually!
:waittofinish
echo At least one process is still running...
timeout /T 2 /nobreak >nul
tasklist.exe /fi "WINDOWTITLE eq !The Title!" | find ":" >nul
if errorlevel 1 goto waittofinish
echo Finished!
PAUSE
回答by johnrefling
'start /w' does NOT work in all cases. The original solution works great. However, on some machines, it is wise to put a delay immediately after starting the executable. Otherwise, the task name may not appear in the task list yet, and the loop will not work as expected (someone pointed that out). The 2 delays can be combined and put at the top of the loop. Can also get rid of the 'else' just to shorten. Example of 2 programs running sequentially:
'start /w' 并非在所有情况下都有效。原始解决方案效果很好。但是,在某些机器上,在启动可执行文件后立即延迟是明智的。否则,任务名称可能还没有出现在任务列表中,循环将无法按预期工作(有人指出)。这 2 个延迟可以组合并放在循环的顶部。也可以去掉 'else' 只是为了缩短。2 个顺序运行的程序示例:
c:\prog1.exe
:loop1
timeout /t 1 /nobreak >nul 2>&1
tasklist | find /i "prog1.exe" >nul 2>&1
if errorlevel 1 goto cont1
goto loop1
:cont1
c:\prog2.exe
:loop2
timeout /t 1 /nobreak >nul 2>&1
tasklist | find /i "prog2.exe" >nul 2>&1
if errorlevel 1 goto cont2
goto loop2
:cont2
john refling
约翰·雷林
回答by antoviccia-zam
This is my adaptation johnrefling's. This work also in WindowsXP; in my case i start the same application at the end, because i want reopen it with different parametrs. My application is a WindowForm .NET
这是我的改编 johnrefling 的。这项工作也适用于 WindowsXP;就我而言,我最后启动了相同的应用程序,因为我想用不同的参数重新打开它。我的应用程序是一个 WindowForm .NET
@echo off
taskkill -im:MyApp.exe
:loop1
tasklist | find /i "MyApp.exe" >nul 2>&1
if errorlevel 1 goto cont1
echo "Waiting termination of process..."
:: timeout /t 1 /nobreak >nul 2>&1 ::this don't work in windows XP
:: from: https://stackoverflow.com/questions/1672338/how-to-sleep-for-five-seconds-in-a-batch-file-cmd/33286113#33286113
typeperf "\System\Processor Queue Length" -si 1 -sc 1 >nul s
goto loop1
:cont1
echo "Process terminated, start new application"
START "<SYMBOLIC-TEXT-NAME>" "<full-path-of-MyApp2.exe>" "MyApp2-param1" "MyApp2-param2"
pause
回答by FilipeCanatto
call process1
call process2
in this case the process2 will not begin until process1 have finished.
在这种情况下,process2 在 process1 完成之前不会开始。