gpt4 book ai didi

python - 使用 python 在 unix 中获取正在运行的进程列表的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-28 19:31:39 24 4
gpt4 key购买 nike

我正在尝试:

import commands
print commands.getoutput("ps -u 0")

但它不适用于 os x。os 而不是命令给出相同的输出:用户 PID %CPU %MEM VSZ RSS TT STAT 开始时间命令

仅此而已

最佳答案

这适用于 Mac OS X 10.5.5。注意大写的 -U 选项。也许那是你的问题。

import subprocess
ps = subprocess.Popen("ps -U 0", shell=True, stdout=subprocess.PIPE)
print ps.stdout.read()
ps.stdout.close()
ps.wait()

这是 Python 版本

Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin

关于python - 使用 python 在 unix 中获取正在运行的进程列表的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/160245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com