[英]Find Windows PID of a python script with Windows Command Prompt
I am running two different python scripts running on a windows machine simultaneously and would like to kill one but not the other from the command prompt. 我正在Windows机器上同时运行两个不同的python脚本,并想从命令提示符处杀死一个,但不要杀死另一个。 Using taskkill with the name "python.exe" does not allow me to choose to kill just one of these scripts. 使用名称为“ python.exe”的taskkill不允许我选择仅杀死这些脚本之一。
Is there a way in windows to kill just one of these tasks, determined by the script from which it originated? Windows中是否有一种方法可以仅杀死其中一项任务(由其起源的脚本确定)?
For example: if I run python_process1.py and python_process2.py and would like to kill the .exe associated with just python_process2.py and leave python_process1.py alone. 例如:如果我运行python_process1.py和python_process2.py,并想杀死仅与python_process2.py相关的.exe,而让python_process1.py独处。
UPDATE: the solution below does not kill the process, and the issue still lies in identifying the PID of a process by python script name. 更新:下面的解决方案不会杀死进程,问题仍然在于通过python脚本名称识别进程的PID。 If this is impossible, is there a way to selectively kill python scripts on windows that I am unaware of? 如果这是不可能的,是否有办法在我不知道的Windows上有选择地杀死python脚本?
Thank you. 谢谢。
Using Get-WmiObject and PowerShell you can examine the command line arguments for each process, then pass the selected Process ID to taskkill. 使用Get-WmiObject和PowerShell,您可以检查每个进程的命令行参数,然后将所选的进程ID传递给taskkill。
This example shows how to kill a process executing the script test.py
: 这个例子展示了如何杀死执行脚本test.py
的进程:
PS C:\Users\Administrator> taskkill.exe /F /PID $(Get-WmiObject Win32_Process -Filter "name = 'python.exe'" | Where-Object {$_.CommandLine -like '*.\test.py'} | Select -ExpandProperty ProcessId)
Modify *.\\test.py
to match how you are actually calling each script, and it should work for you 修改*.\\test.py
以匹配您实际调用每个脚本的方式,它应该对您*.\\test.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.