使用Windows命令提示符查找python脚本的Windows PID [英] Find Windows PID of a python script with Windows Command Prompt

查看:441
本文介绍了使用Windows命令提示符查找python脚本的Windows PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows机器上同时运行两个不同的python脚本,并想从命令提示符处杀死一个,但不要杀死另一个。使用名称为 python.exe的taskkill不允许我选择仅杀死这些脚本之一。

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. Using taskkill with the name "python.exe" does not allow me to choose to kill just one of these scripts.

Windows中是否有一种方法可以杀死其中一个脚本?这些任务,是由它起源的脚本决定的?

Is there a way in windows to kill just one of these tasks, determined by the script from which it originated?

例如:如果我运行python_process1.py和python_process2.py并想杀死与.exe相关的

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.

更新:以下解决方案不会终止该进程,而问题仍然在于通过以下方式识别进程的PID python脚本名称。如果这不可能,是否有办法在我不知道的Windows上有选择地杀死python脚本?

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. If this is impossible, is there a way to selectively kill python scripts on windows that I am unaware of?

谢谢。

推荐答案

使用 Get-WmiObject 和PowerShell,您可以检查每个进程的命令行参数,然后将所选的进程ID传递给taskkill。

Using Get-WmiObject and PowerShell you can examine the command line arguments for each process, then pass the selected Process ID to taskkill.

此示例显示如何杀死执行脚本 test.py 的进程:

This example shows how to kill a process executing the script 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)

修改 *。\test.py 匹配您实际调用每个脚本的方式,它应该对您有用

Modify *.\test.py to match how you are actually calling each script, and it should work for you

这篇关于使用Windows命令提示符查找python脚本的Windows PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆