如何在 python 中使用命令行显示特定 uid 或用户名的进程?
How do I show processes for a specific uid or username using command line in python?
for opt, arg in opts:
if opt in ('-u'):
arg = numuid
ps = subprocess.Popen('ps -u ', shell=True, stdout=subprocess.PIPE)
print ps.stdout.read()
这是我目前所拥有的,numuid 应该在 ps -u 之后并且是 uid。但是无论 numuid 是什么,我该怎么做才能读取它?
你只需要将名称作为变量传递,如果你只是想查看或存储输出check_output
是最简单的方法:
from subprocess import check_output
out = check_output(["ps","-u", numuid])
print(out)
对于python 2.6:
from subprocess import PIPE,Popen
p = Popen(["ps","-u", numuid], stdout=PIPE)
out, err= p.communicate()
print(out)
传递参数列表时不需要shell=True
。
for opt, arg in opts:
if opt in ('-u'):
arg = numuid
ps = subprocess.Popen('ps -u ', shell=True, stdout=subprocess.PIPE)
print ps.stdout.read()
这是我目前所拥有的,numuid 应该在 ps -u 之后并且是 uid。但是无论 numuid 是什么,我该怎么做才能读取它?
你只需要将名称作为变量传递,如果你只是想查看或存储输出check_output
是最简单的方法:
from subprocess import check_output
out = check_output(["ps","-u", numuid])
print(out)
对于python 2.6:
from subprocess import PIPE,Popen
p = Popen(["ps","-u", numuid], stdout=PIPE)
out, err= p.communicate()
print(out)
传递参数列表时不需要shell=True
。