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