如何在批处理文件中等待一个进程终止以执行另一个进程

发布于 2024-12-16 19:58:09 字数 178 浏览 1 评论 0原文

如何在批处理文件中等待一个进程终止后再执行另一个进程?假设我有一个进程 notepad.exe,需要在执行 wordpad.exe 之前终止它。然后,当 wordpad.exe 终止时,我需要再次启动 notepad.exe。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

め七分饶幸 2024-12-23 19:58:09

使用start /w programname等待programname结束

START /W notepad
ECHO Back from notepad  
START /W wordpad
ECHO Back from wordpad
START /W notepad

Use start /w programname to wait for the end of programname

START /W notepad
ECHO Back from notepad  
START /W wordpad
ECHO Back from wordpad
START /W notepad
蓝颜夕 2024-12-23 19:58:09

尝试这样的操作...

@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

我使用了 PSLISTPSEXEC,但您也可以使用 TASKKILLTASKLIST>nul 2>&1 只是用来隐藏 PSLIST 的所有输出。 SLEEP 5 行不是必需的,但只是限制检查写字板是否仍在运行的频率。

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.

℉服软 2024-12-23 19:58:09

这是 aphoria 答案的更新版本。

我用 TASKKILL 和 TASKLIST` 替换了 PSLIST 和 PSEXEC。由于它们似乎工作得更好,我无法让 PSLIST 在 Windows 7 中运行。

还用 TIMEOUT 替换了 Sleep。

这就是我让脚本运行良好所需的一切,所有添加的内容都是由发表评论的优秀人员提供的。

此外,如果 .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

This is an updated version of 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.

Also replaced Sleep with TIMEOUT.

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.

@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
鹊巢 2024-12-23 19:58:09

我喜欢“START /W”答案,尽管对于我的情况,我发现了一些更基本的东西。我的进程是控制台应用程序。由于我的无知,我认为我需要一些特殊的 BAT 语法来确保第一个在第二个开始之前完成。然而,BAT 似乎对控制台应用程序和 Windows 应用程序进行了区分,并且执行它们的方式略有不同。 OP 显示窗口应用程序将作为来自 BAT 的异步调用启动。但对于控制台应用程序来说,它们是在与 BAT 本身运行所在的同一命令窗口内同步调用的。

对我来说,实际上最好不要使用“START /W”,因为所有内容都可以在一个命令窗口内运行。关于“START /W”的烦人之处在于它会生成一个新的命令窗口来执行您的控制台应用程序。

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.

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.

活雷疯 2024-12-23 19:58:09
call process1
call process2

在这种情况下,process2 在 process1 完成之前不会开始。

call process1
call process2

in this case the process2 will not begin until process1 have finished.

百思不得你姐 2024-12-23 19:58:09

这很有效,而且更简单。如果去掉 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

This works and is even simpler. If you remove ECHO-s, it will be even smaller:

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
还在原地等你 2024-12-23 19:58:09

这是我对 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

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

@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
顾冷 2024-12-23 19:58:09

“start /w”并非在所有情况下都有效。原来的解决方案效果很好。然而,在某些机器上,明智的做法是在启动可执行文件后立即延迟。否则,任务名称可能还没有出现在任务列表中,并且循环将无法按预期工作(有人指出)。这两个延迟可以组合起来并放在循环的顶部。也可以去掉“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

'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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文