Python实现ps的代码
时间: 2023-03-27 11:03:07 浏览: 290
import os
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
for pid in pids:
try:
with open(os.path.join('/proc', pid, 'cmdline'), 'rb') as f:
cmdline = f.read().decode().replace('\x00', ' ')
with open(os.path.join('/proc', pid, 'stat'), 'rb') as f:
stat = f.read().decode()
stat = stat.split(' ')
stat = [s for s in stat if s]
pid, comm, state = stat[], stat[1], stat[2]
print(f'{pid}\t{state}\t{cmdline}')
except (FileNotFoundError, PermissionError):
pass