windows 如何在批处理脚本中使用 taskkill 杀死子进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1982662/
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 can I kill child process using taskkill in a batch script?
提问by qodeninja
I'm capturing the PID in a variable which I kill later
我正在一个变量中捕获 PID,稍后我将其杀死
IF NOT "%SERVICE_PID%" == 0 taskkill /pid %SERVICE_PID% /t /f
though everytime I do this in a batch file it makes my computer restart because it kills some system process
尽管每次我在批处理文件中执行此操作时,它都会使我的计算机重新启动,因为它会杀死某些系统进程
the service pid should be a user defined service launched from cmd
服务 pid 应该是从 cmd 启动的用户定义的服务
I dont understand why it keeps making my machine croak.
我不明白为什么它总是让我的机器发出吱吱声。
When I run "taskkill /pid %SERVICE_PID% /t /f" on the command line it works fine! =/
当我在命令行上运行“taskkill /pid %SERVICE_PID% /t /f”时它工作正常!=/
help!
帮助!
Setting SERVICE_PID
设置SERVICE_PID
FOR /F "tokens=4 delims= " %%A IN ('sc queryex myservice ^|FIND "PID"')
DO SET SERVICE_PID=%%A
回答by Pierrick
I found that using this will get the proper PID:
我发现使用它会得到正确的 PID:
for /f "tokens=1,2,3,4 delims=/ " %%a in ('sc queryex ServiceName ^|FIND "PID"') do set PID=%%c
taskkill /pid %PID% /t /f
Then it works like a charm.
然后它就像一个魅力。
回答by srinivas
taskkill /fi "IMAGENAME eq idsm.exe" /fi "CPUTIME gt 00:30:00"
taskkill /fi "IMAGENAME eq idsm.exe" /fi "CPUTIME gt 00:30:00"
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
idsm.exe 4556 Console 0 38,328 K
I got the this result, in the same command line how to kill this process, its should run automatically, means how to use this PID for killing
我得到了这个结果,在同一个命令行中如何杀死这个进程,它应该自动运行,意思是如何使用这个 PID 来杀死
回答by interjaz
I found this the most reliable
我发现这是最可靠的
net stop SERVICE_NAME
timeout /t 10
for /f "tokens=1,2,3,4 delims=/ " %%a in ('sc queryex SERVICE_NAME ^|FIND "PID"') do set PID=%%c
if not %PID% == 0 taskkill /pid %PID% /f
timeout /t 10
net start SERVICE_NAME
The timeout is arbitrary. The first timeout gives service a chance to stop itself properly for some extra amount of time. The second timeout allows the operating system to react, otherwise you may end up with
超时是任意的。第一次超时使服务有机会在一些额外的时间内正确地停止自己。第二次超时允许操作系统做出反应,否则你可能会以
The service is starting or stopping. Please try again later.
服务正在启动或停止。请稍后再试。并且服务不会重新启动。
I am checking if service is not stopped by if not %PID% == 0and force killing it if necessary. The /tflag was unnecessary in my case.
我正在检查服务是否没有被if not %PID% == 0停止,并在必要时强制终止它。在我的情况下,/t标志是不必要的。
回答by Mahesh Velaga
Try removing /f
option, it forces to terminate a process, so system processes might get terminated forcefully without notification.
尝试删除/f
选项,它会强制终止进程,因此系统进程可能会在没有通知的情况下被强制终止。
I am suspecting you are trying to kill some system processes in the batch script, in the sense that in your list of PIDs there might be some system process IDs as well.
我怀疑您正在尝试杀死批处理脚本中的某些系统进程,因为在您的 PID 列表中可能还有一些系统进程 ID。
Thanks
谢谢
回答by PA.
Make sure you have enabled delayed expansion. See the HELP SET for an explanation.
确保您已启用延迟扩展。有关说明,请参阅帮助集。
Insert this line
插入这一行
SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEDELAYEDEXPANSION
as the first line of your batch file.
作为批处理文件的第一行。
and use !SERVICE_PID!
instead of %SERVICE_PID%
to get the environment variable.
并使用!SERVICE_PID!
而不是%SERVICE_PID%
获取环境变量。