简体   繁体   English

使用Windows命令提示符查找python脚本的Windows PID

[英]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.

相关问题 Python:从python脚本的Windows命令提示符中执行windows命令 - Python: Executing the windows command in windows command prompt from python script 重新启动脚本后命令提示符(Windows)和 python 脚本出现问题 - Issue with command prompt (windows) and python script after restarting script 在Windows命令提示中运行Python脚本时如何修复NameError - How to fix NameError when running Python Script in Windows Command Prompt 如何在命令提示符Windows 10中运行python脚本 - How to run python script in Command Prompt Windows 10 在 Anaconda 命令提示符中运行 Python 脚本的 Windows 快捷方式 - Windows Shortcut to Run Python Script in Anaconda Command Prompt python脚本打开Windows命令提示符并打印一些字符串 - python script to open windows command prompt and print some strings 如何在Windows命令提示符下执行sql Python脚本? - How to execute sql Python script in Windows command Prompt? Python 在 Windows 上操作命令提示符 - Python manipulating command prompt on windows 如何在python(Windows)中通过命令行获取PID? - How to get PID by command line in python (windows)? Python:无法使用Python脚本打开新的命令提示符会话并通过命令行(Windows)运行程序 - Python: Unable to open a new command prompt session and run a program through command line (windows) using Python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM