windows 从批处理文件中,如何在调用 taskkill.exe 后等待进程退出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7861683/
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
from a batch file, how can I wait for a process to exit, after calling taskkill.exe?
提问by Cheeso
I want to write a batch file that updates a DLL that is in use by a running process, a regular application.
我想编写一个批处理文件来更新正在运行的进程(常规应用程序)正在使用的 DLL。
To do this, the plan is to stop the process, copy the DLL to the required location, then restart the process.
为此,计划是停止进程,将 DLL 复制到所需位置,然后重新启动进程。
I know I can try to kill a process with taskkill
. How can I make sure the process has fallen over and died, after I shoot it?
我知道我可以尝试使用taskkill
. 在我拍摄之后,如何确保该过程已经失败并死亡?
回答by Cheeso
Here's what I used. It's a subroutine in a batch file.
这是我使用的。它是批处理文件中的一个子程序。
set tasklist=%windir%\System32\tasklist.exe
set taskkill=%windir%\System32\taskkill.exe
-------------------------------------------------------
:STOPPROC
set wasStopped=0
set procFound=0
set notFound_result=ERROR:
set procName=%1
for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do (
if NOT %%A==%notFound_result% (set procFound=1)
)
if %procFound%==0 (
echo The process was not running.
goto :EOF
)
set wasStopped=1
set ignore_result=INFO:
:CHECKDEAD
"%windir%\system32\timeout.exe" 3 /NOBREAK
for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do (
if not %%A==%ignore_result% (goto :CHECKDEAD)
)
goto :EOF
-------------------------------------------------------
To use it from within a batch file, do like this:
要在批处理文件中使用它,请执行以下操作:
call :STOPPROC notepad.exe
Full example:
完整示例:
set tasklist=%windir%\System32\tasklist.exe
set taskkill=%windir%\System32\taskkill.exe
-------------------------------------------------------
:STOPPROC
set wasStopped=0
set procFound=0
set notFound_result=ERROR:
set procName=%1
for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do (
if NOT %%A==%notFound_result% (set procFound=1)
)
if %procFound%==0 (
echo The process was not running.
goto :EOF
)
set wasStopped=1
set ignore_result=INFO:
:CHECKDEAD
"%windir%\system32\timeout.exe" 3 /NOBREAK
for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do (
if not %%A==%ignore_result% (goto :CHECKDEAD)
)
goto :EOF
-------------------------------------------------------
:MAIN
call :STOPPROC notepad.exe
call :STOPPROC Skype.exe
You'll notice lines that have all dashes - that's not a legal syntax for a batch file of course. But, those lines are never reached, because of the use of GOTO statements, so the syntax is never evaluated. Therefore those lines aren't a problem.
您会注意到全是破折号的行——这当然不是批处理文件的合法语法。但是,由于使用了 GOTO 语句,因此永远不会到达这些行,因此永远不会评估语法。因此,这些线不是问题。