简体   繁体   English

我怎么知道 Windows 中正在运行哪些 python 脚本?

[英]How do I know what python scripts are running in Windows?

As I mentioned above, is there a way to find out what python scripts are running in Windows?正如我上面提到的,有没有办法找出Windows中运行的python脚本?

If you have PowerShell installed, you can get that information by using Windows Management Instrumentation (WMI) and some scripting...如果您安装了 PowerShell,则可以使用 Windows Management Instrumentation (WMI) 和一些脚本来获取该信息...

Open the PowerShell and use these two lines, it should could you started:打开PowerShell并使用这两行,你应该可以开始:

> $pys = get-wmiobject Win32_process -filter "Name='python.exe'"
> $pys.CommandLine

This will show you the command line arguments used to start the python process, which should contain the name of the main script file ran by Python.这将显示用于启动 python 进程的命令行参数,其中应包含 Python 运行的主脚本文件的名称。 For a test program I have, it displays the following:对于我拥有的测试程序,它显示以下内容:

"C:\Python27\python.exe" "D:\Projects\wait.py"

In case you have multiple scripts running, the var $pys will be an array, so to access it you'll have to access the individual elements like so:如果您有多个脚本正在运行,var $pys将是一个数组,因此要访问它,您必须像这样访问各个元素:

> $pys[0].CommandLine

EDIT: Or you could do it all in one single line, again in PowerShell:编辑:或者您可以在一行中完成所有操作,再次在 PowerShell 中:

 > get-wmiobject Win32_process -filter "Name='python.exe'" | foreach -process {$_.CommandLine}

I hope you get the general idea.我希望你能大致了解。

This is what you need这就是你需要的

import subprocess
p = subprocess.Popen(["powershell.exe", "get-wmiobject Win32_process -filter \"Name='python.exe'\"| foreach -process {$_.CommandLine}"], stdout=subprocess.PIPE)
out, err = p.communicate()
service = "file.py"
if service in str(out):
   print('Service Is Running')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的 python 后端脚本在 Microsoft Windows 上永远运行? - How do I keep my python backend scripts running forever on Microsoft Windows? 我如何知道 taskmgr 中正在运行哪个 python 脚本? - How do I know which python script is running in taskmgr? 如何知道Windows中的进程是否正在python中运行 - How to know whether a process in windows is running or not in python 如何知道运行的线程是什么:python - How to know what are the threads running : python 我如何知道在Python中使用哪种数据类型? - How do I know what data type to use in Python? 我如何获得 Python 以了解用户连接的 Wifi? - How do I get Python to know what Wifi the user is connected to? 运行我的机器人的python代码会返回语法错误,但我不知道该错误是什么 - Running the python code of my robot returned a syntax error and I do not know what the error is 如何在Windows上的Docker中运行Tensorflow运行python脚本? - How do I run a python script with Tensorflow running in a Docker on Windows? 如何检测我的 Python 脚本是否在“Windows 终端”中运行 - How do I detect if my Python script is running in the 'Windows Terminal' 我怎么知道我的python脚本是否正在运行? (使用Cygwin或Windows Shell) - How can I know if my python script is running? (using Cygwin or Windows shell)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM